【问题标题】:Refresh the datagrid in endedit event using windows application使用 Windows 应用程序刷新 endedit 事件中的数据网格
【发布时间】:2011-12-15 10:49:18
【问题描述】:
  1. 在表单加载时绑定数据网格。

    DataGrid1.DataSource = objBindinglist
    
  2. 改变了datagrid中的值

    DataGrid1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
       DataGridViewCell cell = null;
    
       if (e.RowIndex > -1 && e.ColumnIndex > -1)
       {
         cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
        ((DataGridView)sender).Rows[e.RowIndex].Cells[sates.Index].Value = cell;
       }
    }
    
  3. 编辑后我想刷新数据网格。

    private void DataGrid1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
      ((DataGridView)sender).DataSource = null;
      var lstDataSource = 
        ((BindingList<person>)(((DataGridView)sender).DataSource))
        .OrderByDescending(x => x.sates).ToList();
    objBindinglist = new BindingList<person>(lstDataSource);
    DataGrid1.DataSource = objBindinglist;
    
  4. 在顶部声明一个绑定列表。

     BindingList<person> objBindinglist = new BindingList<person>();
    

问题: 是否可以将刷新列表绑定到有界数据网格。 当我在 endedit 事件中给 datasource 为 null 时,它会给出错误。

【问题讨论】:

    标签: c# winforms c#-4.0 datagridview


    【解决方案1】:

    我认为您将很难在 EndEdit 事件中更改 DataSource,因为网格很可能仍需要这些数据。

    如果您的目标是简单地使用数据,那么使用内置排序机制会更好。配置网格时,在SortColumn 属性中设置您希望数据排序的列,并将SortOrder 属性设置为System.Windows.Forms.SortOrder.Descending

    如果编辑完成后网格没有正确更新,只需调用Sort方法即可。

    【讨论】:

    • 操作无效,因为它会导致对 SetCurrentCellAddressCore 函数的可重入调用
    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多