why doesn't datagridview refresh?

前端 未结 9 2451
-上瘾入骨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: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)
    }
    

提交回复
热议问题