【问题标题】:How to clear datagrid items in wpf while having button in column of each row如何在每行的列中有按钮时清除wpf中的数据网格项目
【发布时间】:2016-07-26 17:21:53
【问题描述】:

我想删除 wpf 中的数据网格项。

我试过了

 dataGridfix.Items.Clear();
 dataGridfix.Items.Refresh();
 dataGridfix.ItemsSource = null;

但由于每一行都有按钮,它会停止清除数据网格。

你能推荐个好主意吗?

完整代码在这里

   private void showfix() 
   {
         dataGridfix.ItemsSource = null;
         DataTable dt = new DataTable();
         dt.Columns.Add("ID");
         dt.Columns.Add("Account");

         SqlDataAdapter msadapt = new SqlDataAdapter("select * from fdetails  order by fdid desc", mscon);
         DataTable msdata = new DataTable();
         msadapt.Fill(msdata);

         for (int i = 0; i < msdata.Rows.Count; i++) 
         {
             string id = msdata.Rows[i].ItemArray[0].ToString();
             string acc = msdata.Rows[i].ItemArray[2].ToString();
             dt.Rows.Add(id, acc);
         }

         dataGridfix.ItemsSource = dt.DefaultView;
         dataGridfix.Columns[0].Width = 50;
         dataGridfix.Columns[1].Width = 50;
         dataGridfix.Columns[2].Width = 180;
     }

xaml 代码是

                                <DataGridTemplateColumn >
                                    <DataGridTemplateColumn.CellTemplate >
                                        <DataTemplate>
                                            <Button Name="btndelfix" Click="btndelfix_Click" >Delete</Button>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>

【问题讨论】:

  • 你能发布 xaml 代码吗?或者清理失败情况的截图?
  • 发布你的DataGrid XAML,我们应该知道你的按钮是如何添加到DataGrid 我想你应该有一个DetaTemplate 来放置按钮。
  • 是的,你是对的,阿宾。我有放置按钮的数据模板。

标签: c# wpf datagrid


【解决方案1】:

你有没有试过做dataGridfix.ItemsSource = null然后做 dataGridfix.Items.Refresh() 而不是 as seen here 的其他方式? 您需要清除 itemsSource 属性才能使绑定停止工作。那么没有什么可以阻止您清空数据网格。 对我来说,使用按钮的 DataTemplate 与您的问题无关。

【讨论】:

    猜你喜欢
    • 2012-08-28
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多