0
votes

I'm struggling with with accepting changes to my datagridview.

I have a listbox and a datagridview. My datagridview changes based on the selected index which is selecte from the listbox. However, each time I select a different item, the datagridview items goes back to the original view/list.

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?

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;
    }
3
I guess you want to filter your source without refreshing it. right? - Badiparmagi
100% correct. And then export/save the datagridview because the last column is a combobox with options PASS & FAIL. - Bennelito

3 Answers

0
votes
dataGridView1.databind();

needs to called for update in gridview for new changes i think.

0
votes

Just set Filter property of bindingsource. But you need to declare bindingsource as you can use from any method in your class.

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

so if you want to export your source, dont refresh it, filter it. BindingSource has a .Filter method which will help you. 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;
}