Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to write values to datagridview Column 2 which is in Form2(Form2 called in Mainform).

but I to want fill coloum2 rows from thread(required).
I have used delegate to avoid cross thread operation

but the datagridview1 rows count is always 1 in threadclass and on form2 it shows right count.
why datagridview1 shows one count always.

I don't know where the problem is

// Form 2 is called in MainForm

Form2
In form 2 there is button which is used to dynamicaly add rows in gridview

Datagridview1.Rows.Add(Convert.ToInt32(textbox1.Text));

// and I fill Sr.no colum by following code e.g 1,2,3
XML
for (int i = 0, j = 1; i <=  Datagridview1.RowCount-1 ; j++, i++)
               {

                   Datagridview1.Rows[i].Cells[0].Value = (j).ToString();
               }
               //this is done to handle error; bool variable 

Form2HandleCreated= true;</pre>
ThreadClass

My thread class objects starts in InitialStartup Form

//this delegate is used to write values to datagrivew which is in form2
public delegate void Del_WriteValueToGridView(int count, string Value);
C#
if (Form2HandleCreated== true)
                   {
                     for (int i = 0; i <= 10; i++)
                      {
                         SR = i;
                         Value= i.ToString();

                          Program.Form2Obj.TopLevel = true;
                          Program.MainFormObj.Labe11.Invoke(new Del_WriteValueToGridView(WriteToGridView), SR, Value);
                            if (i > 10) ;
                            Form2HandleCreated = false;
                        }


C#
void WriteToGridView(int count,string Value)
      {
         //this count is always 1.Why?
          int n= Program.Form2Obj.DataGridView1.Rows.Count;
          Program.Form2Obj.DataGridView1.Rows[count].Cells[1].Value = Value;
      }
Posted

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