I have some problem on getting the value of a specific row of the datagrid when I click the button. Does anyone know if there is another way of getting the value of the datagrid when button is clicked.
My XAML Datagrid Code:
<DataGrid Grid.Row="2" x:Name="coursesList" Margin="20"
AlternationCount="2" GridLinesVisibility="Horizontal"
VerticalGridLinesBrush="#00000000"
HorizontalGridLinesBrush="#FFD6D6D6"
AlternatingRowBackground="#FFE2E2E2"
RowDetailsVisibilityMode="Collapsed"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="40"
Binding="{Binding Path='id'}"/>
<DataGridTextColumn Header="Code" Width="100"
Binding="{Binding Path='courseCode'}"/>
<DataGridTextColumn Header="Descriptive Title" Width="*"
Binding="{Binding Path='descriptiveTitle'}"/>
<DataGridTextColumn Header="Lec" Width="50"
Binding="{Binding Path='lec'}"/>
<DataGridTextColumn Header="Lab" Width="50"
Binding="{Binding Path='lab'}"/>
<DataGridTextColumn Header="Units" Width="61"
Binding="{Binding Path='units'}"/>
<DataGridTextColumn Header="Instructor" Width="*"
Binding="{Binding Path='instructor'}"/>
<DataGridTemplateColumn Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="editCourseBtn" Content="Edit" Click="editCourseBtn_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My C# code:
private void editCourseBtn_Click(object sender, RoutedEventArgs e)
{
try
{
DataRowView dataRowView = (DataRowView)((Button)e.Source).DataContext;
string id = dataRowView[0].ToString();
string courseCode = dataRowView[1].ToString();
string descriptiveTitle = dataRowView[2].ToString();
string lec = dataRowView[3].ToString();
string lab = dataRowView[4].ToString();
string units = dataRowView[5].ToString();
string instructor = dataRowView[6].ToString();
MessageBox.Show(id);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
CodePudding user response:
Cast the sender
argument instead of e.Source
:
DataRowView dataRowView = (DataRowView)((Button)sender).DataContext;
CodePudding user response:
Make a Class or a Model that corresponds the value of the datagrid and use this method instead:
Course edtCourse = ((FrameworkElement)sender).DataContext as Course;
course refers to the class course.
private void editCourseBtn_Click(object sender, RoutedEventArgs e)
{
try
{
Course edtCourse = ((FrameworkElement)sender).DataContext as Course;
int id = edtCourse.id;
string courseCode = edtCourse.courseCode;
string descriptiveTitle = edtCourse.descriptiveTitle;
int lec = edtCourse.lec;
int lab = edtCourse.lab;
int units = edtCourse.units;
string instructor = edtCourse.instructor;
EditCourse edtCourseWindow = new EditCourse();
edtCourseWindow.courseCoudeTxtBox.Text = courseCode;
edtCourseWindow.descriptiveTitleTxtBox.Text = descriptiveTitle;
edtCourseWindow.lecTxtBox.Text = lec.ToString();
edtCourseWindow.labTxtBox.Text = lab.ToString();
edtCourseWindow.unitsTxtBox.Text = units.ToString();
edtCourseWindow.instructorTxtBox.Text = instructor;
edtCourseWindow.idParameter.Text = id.ToString();
edtCourseWindow.ShowDialog();
fetchCourses();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}