【问题标题】:Accept changes on datagridview as selected item in Listbox change接受 datagridview 上的更改作为列表框更改中的选定项
【发布时间】:2017-01-24 11:07:33
【问题描述】:

我正在努力接受对我的 datagridview 的更改。

我有一个列表框和一个数据网格视图。我的数据网格视图根据从列表框中选择的选定索引进行更改。但是,每次我选择不同的项目时,datagridview 项目都会返回到原始视图/列表。

我的问题:如何在每次从列表框中选择项目时接受/将更改写回我的数据表或防止数据网格视图刷新?

我的列表框更改事件的代码是:

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataRow[] result = ds.Tables["AssessmentItems"].Select("GroupId = " + listBox1.SelectedIndex);

        var newTable = result.CopyToDataTable();
        BindingSource bindSource = new BindingSource();
        bindSource.DataSource = newTable;
        dataGridView1.DataSource = bindSource;
    }

【问题讨论】:

  • 我猜你想过滤你的源而不刷新它。对吗?
  • 100% 正确。然后导出/保存 datagridview,因为最后一列是一个带有选项 PASS 和 FAIL 的组合框。

标签: c# datagridview listbox


【解决方案1】:

所以如果你想导出你的源,不要刷新它,过滤它。 BindingSource 有一个 .Filter method 可以帮助你。将您的活动更改为此。完成!

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindingSource bindSource = new BindingSource();
    bindSource.DataSource = yourOrginalSource;
    dataGridView1.DataSource = bindSource;
    //set your bind source filter
    string myFilter = "GroupId = " + listBox1.SelectedIndex;
    source.Filter = myFilter;
}

【讨论】:

    【解决方案2】:
    dataGridView1.databind();
    

    我认为需要在 gridview 中更新新的变化。

    【讨论】:

      【解决方案3】:

      只需设置bindingsourceFilter 属性。但是您需要声明 bindingsource,因为您可以从类中的任何方法中使用。

      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          bindSource.Filter = "GroupId = " + listBox1.SelectedIndex;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-16
        相关资源
        最近更新 更多