First I should say i saw this link :
How to refresh datagridview when closing child form?
And i did like this:
(i have datagridview in Form1)
Form1:
public void FillDataGridView(DataTable dt1)
{
bindingSource1.DataSource = dt1;
bindingSource1.ResetBindings(false);
dataGridView1.DataSource = bindingSource1;
//here i checked number of rows of dt1 and it shows the correct value
}
Form2:
SqlConnection cnn = new SqlConnection(@"My connection string");
private Form1 Handled_frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
Handled_frm1 = frm1;
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
dt.Clear();
using (SqlCommand cmd =cnn.CreateCommand())
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;
cmd.CommandText = "spSearchCustomerByName";
SqlParameter inparam1 = cmd.Parameters.AddWithValue("@name", textbox1.Text);
inparam1.Direction = ParameterDirection.Input;
dap.SelectCommand = cmd;
dap.Fill(dt);
Handled_frm1.FillDataGridView(dt);
}
But the value Of Datagridview does not change!
Edited:
I wanted to test that if i can clear datagrid view or not,so i changed FillDataGridView like this :
public void FillDataGridView(DataTable dt1)
{
dt.Clear();
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
but it does not clear datagridview1!!!
Best Answer
I Used mistakenly an incorrect instance of Form1!!
in form1,there is a button that when click it,it shows form2.i wrote this code in click event of it:
and this was incorrect,because i made additional instance of Form1.
And I Change the code like this: