How to refresh DataGridView?

旧时模样 提交于 2019-12-25 03:58:23

问题


I am trying to force DataGrid to refresh its content on a form closing event. I have tried various methods, searched this forum for answers but still I am unable to find a solution.

This is how my DataGridView is being populated :

string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
string strSQL = "SQL QUERY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bindingSource1;

I have tried the Invalidate method, BindingSource.ResetBindings but still the GridView doesn't refresh. How do I force refresh it?


回答1:


Data refresh

I think you are talking about data refresh. In this case you can just rebind your DatagridView to the source:

DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = aDataSource;

//Update Data in aDataSource
//...

dataGridView1 = null;
dataGridView1.DataSource = aDataSource;

In your code snapshot aDataSource should actually be bindingSource1. As @GIVE-ME-CHICKEN mentioned, in your case you should enclose the re-binding inside the proper callback to attach it to the right event:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e){ ... }

Data refresh

If you don't want to rebind your source you can adopt another solution: binding your dataGridView1 to a Collection Observable. In this case, it will be enough to implement the INotifyCollectionChanged interface to notify the change to its listeners (your DataGridView component)

Graphic refresh

If you are searching only for a graphic refresh, there is a specific method:

dataGridView1.Refresh();



回答2:


Can you try and change this line:

bindingSource1.DataSource = table;

to

bindingSource1.DataSource = table.DefaultView;



回答3:


Maybe doing it similar to this way helps?

public DataTable GetData(string SQLQuery)
{
   string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
   string strSQL = "SQL QUERY";
   SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
   SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
   DataTable table = new DataTable();
   table.Locale = System.Globalization.CultureInfo.InvariantCulture;
   dataAdapter.Fill(table);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   //Maybe only one of the clear code works.
   dataGridView1.Rows.Clear();
   dataGridView1.DataSource = null; 

   BindingSource bindingSource1 = new BindingSource();
   bindingSource1.DataSource = GetData("SQL QUERY");
   dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
   dataGridView1.ReadOnly = true; //<-----------Try taking this off aswell
   dataGridView1.DataSource = bindingSource1;
}


来源:https://stackoverflow.com/questions/23906271/how-to-refresh-datagridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!