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:
<Grid>
<RadioButton x:Name="rb_1" Content="RadioButton" HorizontalAlignment="Left" Margin="351,85,0,0" VerticalAlignment="Top" GroupName="1" />
<RadioButton x:Name="rb_2" Content="RadioButton" HorizontalAlignment="Left" Margin="351,105,0,0" VerticalAlignment="Top" GroupName="1"/>
<RadioButton x:Name="rb_3" Content="RadioButton" HorizontalAlignment="Left" Margin="351,120,0,0" VerticalAlignment="Top" GroupName="1" />
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,159,0,0" VerticalAlignment="Top" GroupName="2" />
<RadioButton x:Name="rb_4" Content="RadioButton" HorizontalAlignment="Left" Margin="351,179,0,0" VerticalAlignment="Top" GroupName="2"/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,199,0,0" VerticalAlignment="Top" GroupName="2" />
<Button Content="Button" HorizontalAlignment="Left" Margin="713,60,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
<DataGrid x:Name="datagrid_" HorizontalAlignment="Left" Margin="549,85,0,0" VerticalAlignment="Top" Height="253" Width="399" />
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,226,0,0" VerticalAlignment="Top" GroupName="3" />
<RadioButton x:Name="rb_6" Content="RadioButton" HorizontalAlignment="Left" Margin="351,246,0,0" VerticalAlignment="Top" GroupName="3"/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,266,0,0" VerticalAlignment="Top" GroupName="3" />
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,298,0,0" VerticalAlignment="Top" GroupName="4" />
<RadioButton x:Name="rb_8" Content="RadioButton" HorizontalAlignment="Left" Margin="351,318,0,0" VerticalAlignment="Top" GroupName="4"/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="351,338,0,0" VerticalAlignment="Top" GroupName="4" />
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;
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_6.IsChecked == true)
{
j = 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;
}
if (rb_8.IsChecked == true)
{
j = 3;
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;
}
count++;
}
}