Click here to Skip to main content
15,843,865 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to fill value in datagridview of a particular column value from different thread.

Delegate Updates textbox values but not gridview.

I tried using delegate thread but it does not update.

When I check the value of RowCount in delegate method RowCount is always one.but on mainform Rowcount is correct.

How to update row by row value in datagrid

I tried following code
    void Run ( )
        for ( int i = 0; ( i <= 10 ); i++ )
            SR = i;
            Value = i.ToString ( );
            Program.MainFormObj.Datagridview1.Invoke ( 
                new Del_WriteValueToGridView ( 
                        WriteToGridView ), 
                Value );

    void WriteToGridView ( int    count,
                           string Value)
                                    //column 1
                         Rows [ count ].
                         Cells [ 1 ].Value = Value;

I also tried writeValueTogridview() method in mainform and then directly calling this method in
threadclass but not work.
Updated 16-May-14 7:16am
gggustafson 16-May-14 12:21pm    
Formatting issue: replaced <pre lang="xml"> with <pre lang="cs">. Always use the proper language for the code.

Code: In Run, I do not see the need for SR or Value. They should be replaced by i and i.ToString ( ). Why are you using a thread at all? Why don't you access datagridview1 directly?
krishpraj123 16-May-14 19:45pm    
I want a specific column values from other source which I am receving from other dll
my datagridview is in mainform. So i have use thread to update the values.and a delegate to avoid cross thread operation.
so that user can see the values changing it is dynamic.

1 solution


To write from a diffrent thread you have to use delegates: here is how you can achive your goal:

                dataGridView1.DataSource = null;
                dataGridView1.DataSource = myList.ToList();

I generaly set the datagridview to null before doing any kind of operation to be sure the dataGrid is repainted.

i tryed using dataGridView1.Refresh(), Repaint() ... nothing works.

the best method to refresh a dataGridView is to do as described earlier.

Hope it helps.

PS: you can use delegates in diffrents way, but this one is generaly the easiest/fastest.

By the way, Now that you have implemented delegates, you have to make sure the thread is terminated before the form is closed, otherwise you will have an exception.
Share this answer
krishpraj123 16-May-14 20:02pm    
Thankyou for your reply.
I am getting error..
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

In my code you can see I have used delegate to write values

Del_WriteValueToGridView is my delegate and I am passing method WriteToGridView(row,value) to it.
Whats wrong with this code and with this code I don't get above error.
krishpraj123 16-May-14 20:44pm    
I tried to solve error "Invoke or BeginInvoke cannot be called on a control until the window handle has been created." by creating form handle.

but my IsHandleCreated is still false and also IsDisposed is false(Isdisposed Shoulb be false).

myform is in Mainform. i.e I have called myform in Mainform in a panel and is myform.Toplevel=false.

But Mainform.IsHandleCreated is true.
So to check your code I used MainformObjects its showing blank datagridview.
Code is not working at all
Ziee-M 19-May-14 7:15am    
Hello Krish, sorry i diden't open codeproject for a while, The exeption you r facing means the form is still not created or the controll is alerady diposed (just like i said in my final line of the solution).

Now, the order of execution of methods is important.

You have to be sure all the contolles in the form have been created before using delegates to acces a controle. so starting from Page_Load method, you should be okay.

Also, try doing somting like writing in a label using a delagte to understand the idea and test.

By the way i just noticed, but :
DataGridView1.Rows [ count - 1].Cells [ 1 ].Value = Value; // you have to add -1 to the count since the start pos is 0

Waiting for your response.

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