【问题标题】:Delete and Edit a datagrid row using checkboxes in WPF C# (Data coming from database)使用 WPF C# 中的复选框删除和编辑数据网格行(来自数据库的数据)
【发布时间】:2020-12-13 14:33:22
【问题描述】:

我目前是 C#(WPF) 的新手,正在尝试开发一个项目并坚持这一点。我想为每一行添加复选框(以及标题中的主复选框)。数据来自我的数据库到数据网格。我希望能够一次检查一个复选框,如果我单击主复选框,则所有其他复选框也应该被选中。我希望能够对一行执行 DELETE、EDIT。也删除所有选定的行。 我的 XMAL 示例代码是:

 <DataGrid x:Name="dataGrid" CanUserAddRows="False" ColumnWidth="*"  Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="5" Grid.RowSpan="9"></DataGrid>

我的 CS 示例代码是:

 public void updateDataGrid()
    {
        string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Zeeshan\Documents\zeemobiles.mdf;Integrated Security=True;Connect Timeout=30";

        string query = "Select * from Addphones";

        SqlConnection databaseConnection = new SqlConnection(connectionString);
        SqlCommand commandDatabase = new SqlCommand(query, databaseConnection);
        SqlDataAdapter ad = new SqlDataAdapter(commandDatabase);
        commandDatabase.CommandTimeout = 60;

        // Let's do it !
        try
        {

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Selected", typeof(bool))); //this will show checkboxes
            
            ad.Fill(dt);

            dataGrid.ItemsSource = dt.DefaultView;

            ad.Update(dt);
        }
        catch (Exception ex)
        {
            // Show any error message.
            MessageBox.Show(ex.Message);
        }
        finally
        {
            databaseConnection.Close();
        }

    }

这是我从不同网站获得帮助后得出的结论。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    另一种更好的方法是从DataGridView 中选择项目。并对其进行操作。但为此,您还需要指定项目来源,以及定义列和您将使用Binding="{Binding id}" in DataGridTextColumn 绑定的数据。

    更多: https://www.c-sharpcorner.com/UploadFile/009464/how-to-bind-datagrid-in-wpf-using-C-Sharp/

    【讨论】:

      【解决方案2】:

      视图模型

      public class ViewModel
      {
          public IList<ItemViewModel> DataSource { get; }
      
          public ICommand EditCommand { get; set; }
      
          public ICommand DeleteCommand { get; set; }
      
          private void DeleteCommandExecuteDelegate(object obj)
          {
              foreach (var item in DataSource)
              {
                  if (item.IsSelected)
                  {
                      DataSource.Remove(item);
                  }
              }
          }
      
          private void EditCommandExecuteDelegate(object obj)
          {
              foreach (var item in DataSource)
              {
                  if (item.IsSelected)
                  {
                      item.Property1 = Guid.NewGuid();
                  }
              }
          }
      }
      
      public class ItemViewModel
      {
          public bool IsSelected { get; set; }
      
          public object Property1 { get; set; }
      }
      

      查看

      <DataGrid x:Name="DataGrid1" ItemsSource="{Binding Path=DataSource}">
          <DataGridTemplateColumn Header="Checked" >
              <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                      <StackPanel Orientation="Horizontal">
                          <CheckBox IsChecked="{binding Path=IsSelected"/>
                      </StackPanel>
                  </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>    
      </DataGrid>
      

      演示

      DataGrid1.DataContext = new ViewModel();
      

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      • 感谢您的 cmets。我更改了答案! @阿米尔多拉
      • 您在上面分享的代码根本没有链接到我的问题。我想要复选框,而你使用了按钮。
      • @Zero001 我修改了这个逻辑,我给出的代码只是一个例子。实际情况需要你根据自己的需要修改
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 2017-12-05
      • 2021-03-27
      相关资源
      最近更新 更多