Can't refresh datagridview with bindingsource
Asked Answered
E

3

10

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;
Enochenol answered 14/3, 2011 at 20:18 Comment(3)
What is your datagrid bound to?Virnelli
FYI; You shouldn't need to set the dgridStock.DataSource = null; that is just a way of doing it if you haven't implemented a BindingSource!Gills
Does your list item type implement INotifyPropetyChanged interface?Flourish
F
2

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.

Fortification answered 10/5, 2012 at 15:1 Comment(0)
C
1

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();
    }
Copenhagen answered 7/4, 2012 at 20:29 Comment(0)
C
0

You need a list that will inform the BindingSource when an item is added etc. Use a System.ComponentModel.BindingList for that.

Dim lisItems As New System.ComponentModel.BindingList(Of myObject)

Works Great! Only AddRange is missing so this takes care of that:

Private Sub AddRange(ByVal lis As List(Of myObject))
    For Each itm In lis
        lisItems.Add(itm)
    Next
End Sub

https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.bindinglist-1?view=netframework-4.7.2

Cleopatracleopatre answered 24/11, 2018 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.