Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't refresh datagridview with bindingsource

Goal:
Once clicking on add or delete button, the datagridview should be refreshed with the latest data from document.

Problem:

The datagridview can't be refreshed after making changes by deleting or adding new data.

I'm using binding source that is linked with datagridview's datasource.

I tried everything with different solution and read advise from different forum but still I can't solve this problem.

I also tried using these syntax "BindingSource.ResetBindings(false)", "BindingSource.Refresh()" etc but no result.

Links below:

How to refresh a bindingsource

http://www.eggheadcafe.com/community/aspnet/2/10114324/datagridview-refresh-from-another-form.aspx

http://blogs.msdn.com/b/dchandnani/archive/2005/03/15/396387.aspx

http://bytes.com/topic/c-sharp/answers/812061-problem-refresh-datagridview

    bSrcStock.DataSource = myProductrepository.GetAllProductList();


    dgridStock.DataSource = null;
    dgridStock.DataSource = bSrcStock;
    bSrcStock.ResetBindings(true);


    dgridStock.Columns[0].Width = 101;
    dgridStock.Columns[1].Width = 65;
    dgridStock.Columns[2].Width = 80;
    dgridStock.Columns[3].Width = 120;
    dgridStock.Columns[4].Width = 90;
like image 402
What'sUP Avatar asked Mar 14 '11 20:03

What'sUP


2 Answers

I have faced this same issue and found out that the problem is with the initialization of the BindingSource inside a static constructor (The class was a singleton). Upon realizing this, I moved the code to the calling event and it finally worked without needing to assign null or call the clear method. Hope this helps.

like image 86
anna Avatar answered Nov 10 '22 15:11

anna


No need to define the columns (unless you really want to...)

Then just call for the refreshDataGridView Method every time you add or remove something from your list...

    public List<CustomItem> ciList = new List<CustomItem>();

    CustomItem tempItem = new CustomItem();
    tempItem.Name = "Test Name";

    ciList.add(tempItem);
    refreshDataGridView();

    private void refreshDataGridView()
    {
        dataGridView1.DataSource = typeof(List<>);
        dataGridView1.DataSource = ciList;
        dataGridView1.AutoResizeColumns();
        dataGridView1.Refresh();
    }
like image 1
John Bartels Avatar answered Nov 10 '22 15:11

John Bartels