Don't do this:
DataTable dt;
DataRow dr;
string[] str = new string[4];
int location = 0;
int count = 0;
Always use explicit access modifiers.
Also, do any of those fields really need to be global?
Now, the real problem is that you don't follow MVVM. A lot of my other issues follow from that.
For instance:
<Button Content="Button" HorizontalAlignment="Left" Margin="713,60,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
You shouldn't use the Click of a button; instead you should bind it to a command.
You also use the name of your DataGrid
in your code behind, datagrid_
. That is a meaningless name that doesn't follow any naming guideline. Quite frankly, I don't think I've used the Name
of a XAML
object more than a handful of times in the years I did Silverlight development: whenever possible you should bind to a source.
I also see a lot of Margin
properties being used in very specific ways. I would urge you to look into the various layout possibilities of XAML and apply those. Don't work pixel-perfect, it's pointless IMHO and just a maintenance nightmare.
You XAML code looks like it's drag & drop. Which is easy to use I guess, but I'd urge you to abandon the visual editor and code your XAML "by hand".
A quick solution for now: look at the code that you repeat over and over, i.e. most of this:
int k = 0;
dr = dt.NewRow();
Label label = new Label();
label.Height = 28;
label.Width = 100;
label.HorizontalAlignment = HorizontalAlignment.Center;
label.VerticalAlignment = VerticalAlignment.Center;
label.Content = str[j];
dr[k] = label.Content;
dt.Rows.Add(dr);
datagrid_.ItemsSource = dt.DefaultView;
location += 34;
Once you start to copy-paste code and simply change one or two things, you know that's a sign you need to move it to a method that will accept the necessary parameters.
- 11.4k
- 2
- 28
- 45