patterncsharpMinor
Insert String array as label content in datagrid row through radio button
Viewed 0 times
insertarrayradiolabelbuttonthroughcontentrowdatagridstring
Problem
I have written some code for inserting a label at runtime that has its content set to a string array into a datagrid row. All of this will initiate when certain radio buttons are checked. The code is working perfectly fine, but I need to improve this code as I am learning C#, WPF and datagrid. I know there can be a certain way to improve this code. This code will be a nightmare when there are 50 radio buttons. Can it be improved?
XAML Code:
Code Behind:
`public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DataTable dt;
DataRow dr;
string[] str = new string[4];
int location = 0;
int count = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dt = new DataTable("emp");
DataColumn dc1 = new DataColumn("Factors", typeof(string));
DataColumn dc2 = new DataColumn("Non_Compliant", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
datagrid_.ItemsSource = dt.DefaultView;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (count >= 1)
{
datagrid_.ItemsSource = dt.DefaultView;
dt.Clear();
}
str[0] = "Load Path1";
str[1] = "Load Path2";
str[2] = "Load Path3";
str[3] = "Load Path4";
int j = 0;
if (rb_2.IsChecked == true)
{
j = 0;
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;
}
if (rb_4.IsChecked == true)
{
j = 1;
XAML Code:
Code Behind:
`public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DataTable dt;
DataRow dr;
string[] str = new string[4];
int location = 0;
int count = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dt = new DataTable("emp");
DataColumn dc1 = new DataColumn("Factors", typeof(string));
DataColumn dc2 = new DataColumn("Non_Compliant", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
datagrid_.ItemsSource = dt.DefaultView;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (count >= 1)
{
datagrid_.ItemsSource = dt.DefaultView;
dt.Clear();
}
str[0] = "Load Path1";
str[1] = "Load Path2";
str[2] = "Load Path3";
str[3] = "Load Path4";
int j = 0;
if (rb_2.IsChecked == true)
{
j = 0;
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;
}
if (rb_4.IsChecked == true)
{
j = 1;
Solution
Don't do this:
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:
You shouldn't use the Click of a button; instead you should bind it to a command.
You also use the name of your
I also see a lot of
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:
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.
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:
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.
Code Snippets
DataTable dt;
DataRow dr;
string[] str = new string[4];
int location = 0;
int count = 0;<Button Content="Button" HorizontalAlignment="Left" Margin="713,60,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>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;Context
StackExchange Code Review Q#86664, answer score: 3
Revisions (0)
No revisions yet.