how to refresh a data grid?
i m using c# as front end and ms access as back end
I m displaying the datagrid only when the index in the combo box is changed
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
refershGridView(comboBox1.Text);
}
but when I make any updates to the data grid ,the update reflects only after i make a selected index change event
i tried datagidview1.refresh() and also called my refershGridView(comboBox1.Text) functions implicitly
but my grid view refreshes only when i make a selected index change
code for refershGridView(comboBox1.Text)
private void refershGridView(string tableName)
{
saveBttnSwitch = 0;//for save button swicth
//setting back to intial user interface ..
clearVisibilty();
clearall();
button1.Visible = true;
button3.Visible = false;
label11.Visible = false;
try
{
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString = ConnString;
//create the database query
string query = null;
if (tableName == "employee")
{
query = "SELECT fname,lname,ssn FROM employee";
dataGridView1.Visible = true;
}
if (tableName == "project")
{
query = "SELECT pname,pnumber FROM project";
dataGridView1.Visible = true;
}
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, mycon);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTa开发者_Go百科ble = new DataTable();
//fill the DataTable
try
{
dAdapter.Fill(dTable);
}
catch (OleDbException exp)
{
label11.Text = "file couldnt be found...kindly check Db file location ";
label11.Visible = true;
button1.Visible = false;
}
// DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dataGridView1.DataSource = bSource;
// dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AutoGenerateColumns = true;
mycon.Close();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new InvalidOperationException("Data could not be read", ex);
}
this.button2.Visible = false;
}
Does refershGridView(comboBox1.Text);
refill the DataGrid?
If so, call it from any other place where you want your data to be refilled. If SelectedIndexChanged even of the combo is the only place you are filling it, it won't refresh unless you change a selection.
And as @Bryan suggests, its better if we see some code.
Try to clear the dTable
and dataGridView1.DataSource
before you fill the DataTable and sets the DataGridView DataSource
dTable = new DataTable();
dataGridView1.DataSource = nothing;
精彩评论