why doesn't datagridview refresh?

前端 未结 9 2223
-上瘾入骨i
-上瘾入骨i 2021-01-02 18:19

here is what happens after i press a button:

    dataGridView1.DataSource = ConnectandReadList(some_query);
    dataGridView1.Refresh();

pl

相关标签:
9条回答
  • 2021-01-02 18:29

    A DataGridView sets up bindings the first time you assign the DataSource. The problem is that subsequent DataSource assignments, if the assignments have a different structure from the initial assignment, will fail because the bindings are now "off"

    You need to reset the DataGridView thusly so that the data is bound a new. (The link is for VB but you just need to know the methods to call. Even copy/paste would be overkill.)

    0 讨论(0)
  • 2021-01-02 18:36

    Subtle difference here to @Fake but calling Refresh() won't work as calling this on the dataGridView only

    "Forces the control to invalidate its client area and immediately redraw itself and any child controls."

    As this method relates to any control, not to the refresh of the data relating to an object. Refer here (DataGridView Methods) and scroll down to Refresh and you will see the link points to Control.Refresh Method

    You want something like this;

    BindingSource bs = new BindingSource(); 
    bs.DataSource = ConnectandReadList(some_query);
    dataGridView1.DataSource = bs;
    bs.ResetBindings(false)
    

    and then you can just call ResetBindings() on bs (Your BindingSource);

    BindingSource bs = new BindingSource(); 
    private refreshData()
    {
        bs.ResetBindings(false)
    }
    
    0 讨论(0)
  • 2021-01-02 18:44

    Little trick you can do if you are binding to a List<> when setting it to your data source add ToList() on it at the end like this:

    dataGridView1.DataSource = ConnectandReadList(some_query).ToList();
    

    For some reason this causes it to refresh without loosing any references or anything.

    An alternative is to directly notify the DataGridView that its data changed like this:

    dataGridView1.DataSource = ConnectandReadList(some_query)
    var m = dataGridView1.GetType().GetMethod("OnDataSourceChanged", BindingFlags.NonPublic | BindingFlags.Instance);
    m.Invoke(dataGridView1, new object[] { EventArgs.Empty });
    
    0 讨论(0)
  • 2021-01-02 18:48

    This line of code loads data into the wMP_EXPORTDataSet.DEST_AX_PRICEDISCADMTRANSENTITY table. You can move, or remove it, as needed.

    this.dEST_AX_PRICEDISCADMTRANSENTITYTableAdapter.Fill(this.wMP_EXPORTDataSet.DEST_AX_PRICEDISCADMTRANSENTITY);
    
    0 讨论(0)
  • 2021-01-02 18:48

    Actually problem is here that your TableAdapter is not refreshing. I used this code after editing table (mydatabase.tbl) to refresh it:

                tblTableAdapter.Fill(mydatabaseDataSet.tbl);
                dataGridView1.DataSource = tblBindingSource;
                dataGridView1.Update();
    
    0 讨论(0)
  • 2021-01-02 18:49

    try this?

    dataGridView1.DataSource = null;
    dataGridView1.DataSource = ConnectandReadList(some_query);
    dataGridView1.Refresh();
    
    0 讨论(0)
提交回复
热议问题