Reputation: 585
I've been reading a lot of answers on SO and I couldn't find a simple one to address my issue. I have a simple datagrid and I've added a checkbox column.
<DataGrid AutoGenerateColumns="False" VirtualizingPanel.IsVirtualizingWhenGrouping="True" EnableColumnVirtualization="True" EnableRowVirtualization="True" VirtualizingPanel.IsVirtualizing="true" ColumnHeaderStyle="{StaticResource lowCase}" x:Name="dtGrid" HorizontalAlignment="Left" CanUserResizeRows="False" ItemsSource="{Binding}" GridLinesVisibility="All" HorizontalContentAlignment="Stretch" CanUserAddRows="false" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Audited"></DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Location}" Header="Location" Visibility="Collapsed"/>
<DataGridTextColumn Binding="{Binding Date, StringFormat=MM-dd-yy}" Header="Date"/>
<DataGridTextColumn Binding="{Binding RegularPaidHours}" Header="Regular Repair Hours"/>
</DataGrid.Columns>
</DataGrid>
My goal is to send an update command everytime a row is checked as audited. Somewhere along the lines of:
MySqlCommand cmd = new MySqlCommand("update MyTable set Location='" + Location + "',Date='" + Date + "',RegularPaidHours='" + RegularPaidHours + "', Audited '"yes" where ID = txt.id' , connection);
So if someone checks a rows, that row gets updated in the db. If not just have it null. Can someone please give me a helping hand with this? I don't really know what's my best option here.
Upvotes: 0
Views: 281
Reputation: 169200
Bind the column to a source property of your class where the Location
, Date
and RegularPaidHours
property are already defined:
<DataGridCheckBoxColumn Header="Audited" Binding="{Binding IsAudited, UpdateSourceTrigger=PropertyChanged}" />
...and do whatever you want in the setter of the property:
private bool _isAuditing;
public bool IsAuditing
{
get { return _isAuditing; }
set
{
_isAuditing = value;
//invoke a command or call a method that updates the DB from here...
Task.Run(() => { UpdateDb(Location, ...) });
}
}
Can send the update command using sql in the code behind? I am looking to do it only with some code-behind.
Then you should replace the DataGridCheckBoxColumn
with a DataGridTemplateColumn
and handle the Checked
and Unchecked
event for the CheckBox
in the template:
<DataGridTemplateColumn Header="Audited" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
IsChecked="{Binding IsAudited, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
IsChecked="{Binding IsAudited, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
private void DataGridCheckBoxColumn_Checked(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
var model = checkBox.DataContext as YourClass;
//update the DB using the properties of your model...
}
Upvotes: 1
Reputation: 1486
<DataGrid AutoGenerateColumns="False" VirtualizingPanel.IsVirtualizingWhenGrouping="True" EnableColumnVirtualization="True" EnableRowVirtualization="True" VirtualizingPanel.IsVirtualizing="true" ColumnHeaderStyle="{StaticResource lowCase}" x:Name="dtGrid" HorizontalAlignment="Left" CanUserResizeRows="False" ItemsSource="{Binding}" GridLinesVisibility="All" HorizontalContentAlignment="Stretch" CanUserAddRows="false" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Audited">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Command="{Here your command. Most Probably Relative Source Binding}" CommandParameter="{Binding}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Location}" Header="Location" Visibility="Collapsed"/>
<DataGridTextColumn Binding="{Binding Date, StringFormat=MM-dd-yy}" Header="Date"/>
<DataGridTextColumn Binding="{Binding RegularPaidHours}" Header="Regular Repair Hours"/>
</DataGrid.Columns>
</DataGrid>
And Define corresponding RelayCommand with proper parameter in View Model
Upvotes: 1