Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i did like this: (i have datagridview in Form1)

Form1:

C#
public void FillDataGridView(DataTable dt1)
  {
//dt1 is datatable
          bindingSource1.DataSource = dt1;
          bindingSource1.ResetBindings(false);
          dataGridView1.DataSource = bindingSource1;
          //here i checked number of rows of dt1 and it shows the correct value
  }


Form2:
C#
  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);
//dt is datatable and dap is dataadapter
                            Handled_frm1.FillDataGridView(dt);
                        }

i dont get any error but datagridview rows does not change also when i checked count of rows where i commented,it shows the correct count!

Edited:

I wanted to test that if i can clear datagrid view or not,so i changed FillDataGridView like this :

C#
public void FillDataGridView(DataTable dt1)
       {
               dt.Clear();
               dataGridView1.Columns.Clear();
               dataGridView1.DataSource = null;
               dataGridView1.Refresh();
       }

but it does not clear datagridview1!!!
Posted
Updated 4-Jun-12 11:50am
v5
Comments
taha bahraminezhad Jooneghani 4-Jun-12 17:20pm    
what is dt and dap?
arashmobileboy 4-Jun-12 17:22pm    
dt=datatable
dap=dataAdapter
arashmobileboy 4-Jun-12 17:51pm    
please see my edited question

1 solution

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:
C#
Form1 frm1=new Form1();
Form2 frm2=new Form2(frm1);

and this was incorrect,because i made additional instance of Form1.

And I Change the code like this:
C#
Form2 frm2=new Form2(this);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900