发送update mysql命令的Datagrid复选框列

时间:2017-10-23 20:34:32

标签: c# wpf datagrid

我一直在阅读很多关于SO的答案,我找不到一个简单的解决我的问题。我有一个简单的数据网格,我添加了一个复选框列。

<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>

我的目标是每次检查一行作为审计时发送更新命令。在某处:

MySqlCommand cmd = new MySqlCommand("update MyTable set Location='" + Location + "',Date='" + Date + "',RegularPaidHours='" + RegularPaidHours + "', Audited '"yes" where ID = txt.id' , connection);

因此,如果有人检查行,则该行会在数据库中更新。如果不是只有它null。有人可以帮我一把吗?我真的不知道这是我最好的选择。

2 个答案:

答案 0 :(得分:1)

<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>

在View Model

中使用适当的参数定义相应的RelayCommand

答案 1 :(得分:1)

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...

}