why doesn't datagridview refresh?

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

提交回复
热议问题