【问题标题】:Datagrid checkbox logic to update table when checked选中时更新表格的Datagrid复选框逻辑
【发布时间】:2018-04-09 08:39:53
【问题描述】:

我无法让我的复选框列正常工作。我想要实现的是,如果条件与表中的值匹配,则在应用程序加载时选中复选框,并在每次选中或取消选中复选框时向数据库发送更新命令。

我应该如何编写它才能让它落地?我希望在没有 MVVM 的情况下实现我的目标。如果有人能帮助我摆脱困境,我将不胜感激。

这是我已经走了多远:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Audited" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox x:Name="cBox" Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
              IsChecked="{Binding Audited, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <CheckBox x:Name="cBox" Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
              IsChecked="{Binding Audited, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding Location}" Header="Location" Visibility="Collapsed"/>
        <DataGridTextColumn Binding="{Binding Date, StringFormat=MM-dd-yy}" Header="Date"/>
    </DataGrid.Columns>
</DataGrid>

xaml.cs

 public MainWindow()
        {
            InitializeComponent();

            string connectionString = "datasource=; Port=; Username=; Password=";
            string sMonth = DateTime.Now.ToString("MM");
            string sYear = DateTime.Now.ToString("yyyy");
            string sDate = DateTime.Now.ToString("yyyy-MM-dd");

            MySqlConnection connection = new MySqlConnection(connectionString);

            MySqlCommand Audit = new MySqlCommand("Select Audited from Daily.Table where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);
            Audit.Parameters.Add(new MySqlParameter("sDate", sDate));

            try
            {
                connection.Open();

                MySqlDataReader AuditR = Audit.ExecuteReader();

                while (AuditR.Read())
                {
                    if (AuditR["Audited"] != DBNull.Value)
                    {
                        //How I can set the checkbox to checked?
                    }; 
                }

                AuditR.Close();
                AuditR.Dispose();


        private void DataGridCheckBoxColumn_Checked(object sender, RoutedEventArgs e)
        {
            string connectionString = "datasource=; Port=; Username=; Password=";

            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited='Yes' where ID= '" + this.txtID.Text + "'", connection);
            CheckBox checkBox = sender as CheckBox;

            //How can I mark the checkbox as checked from here?

        }

        private void DataGridCheckBoxColumn_Unchecked(object sender, RoutedEventArgs e)
        {
            string connectionString = "datasource=; Port=; Username=; Password=";

            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited=NULL where ID= '" + this.txtID.Text + "'", connection);
            CheckBox checkBox = sender as CheckBox;

            //How can I mark the checkbox as unchecked from here?
        }

【问题讨论】:

    标签: c# wpf checkbox datagrid


    【解决方案1】:

    我遇到了类似的问题。我在不使用 MVVM 的情况下解决了它。这样的事情可能会对你有所帮助。

    <DataGridTextColumn Header="Operand" Binding="{Binding Path=MyVal}">
                            <DataGridTextColumn.EditingElementStyle>
                                <Style TargetType="{x:Type CheckBox}">
                                    <EventSetter Event="SelectionChanged" Handler="CheckBox_SelectionChanged" />
                                </Style>
                            </DataGridTextColumn.EditingElementStyle>
                        </DataGridTextColumn>
    

    创建一个包含数据网格视图行的类

    class MyRowItem
    {
        public string ID {get;set;}
            public Checkbox Audited {get;set;}
            public string Location {get;set;}
            public string Date {get;set;}
    
    }
    

    创建一个 MyRowItem 列表并将所有行存储在那里

    List<MyRowItem> rowList = new List<MyRowItem>();
    rowList.Add(new MyRowItem(){ Audited = yourvalue, Location = "yourvalue", Date = "yourvalue"});
    

    现在在 Checkbox_SelectionChanged 事件处理程序中

    private void Checkbox_SelectionChanged(object sender, RoutedEventArgs e)
            {
                string connectionString = "datasource=; Port=; Username=; Password=";
    
                MySqlConnection connection = new MySqlConnection(connectionString);
                MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited='"Yes"' where ID= '" + this.txtID.Text + "'", connection);
                CheckBox checkBox = sender as CheckBox;
    
            foreach(MyRowItem mri in rowList)
            {
            if(mri.ID == "your matching ID")
            {
                //something like this
                mri.Audited = checkBox;
            }
            }
    
            }
    

    最后,

    Datagrid.Source = rowList;
    Datagrid.Refresh();
    

    【讨论】:

    • 感谢 Sourav 的快速回答。你能帮我处理 C# 部分吗?
    • 感谢您的帮助苏拉夫。我已经尝试过了,但无法使列表正常工作(它在当前上下文中不存在),并且 Checkbox_SelectionChanged 引发以下错误:无法识别成员“SelectionChanged”或无法访问。你对此有更简单的看法吗?作为初学者,我很难掌握。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多