简体   繁体   English

接受datagridview上的更改作为列表框更改中的选定项

[英]Accept changes on datagridview as selected item in Listbox change

I'm struggling with with accepting changes to my datagridview. 我正在努力接受对datagridview的更改。

I have a listbox and a datagridview. 我有一个列表框和一个datagridview。 My datagridview changes based on the selected index which is selecte from the listbox. 我的datagridview会根据从列表框中选择的选定索引进行更改。 However, each time I select a different item, the datagridview items goes back to the original view/list. 但是,每次我选择一个不同的项目时,datagridview项目都会返回到原始视图/列表。

My question: How can I accept/write changes back to my datatable or prevent the datagridview from refreshing everytime I select an item from the listbox? 我的问题:每当我从列表框中选择一个项目时,如何接受/将更改写回我的数据表或阻止datagridview刷新?

The code for my listbox change event is: 我的列表框更改事件的代码是:

        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;
    }
dataGridView1.databind();

我认为需要在gridview中进行更新以进行新更改。

Just set Filter property of bindingsource . 只需设置bindingsource Filter属性。 But you need to declare bindingsource as you can use from any method in your class. 但是您需要声明bindingsource,因为您可以在类中的任何方法中使用它。

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

so if you want to export your source, dont refresh it, filter it. 因此,如果要导出源,请不要刷新它,对其进行过滤。 BindingSource has a .Filter method which will help you. BindingSource有一个.Filter 方法可以为您提供帮助。 change your event to this. 将您的活动更改为此。 done! 完成了!

 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM