Hack Forums
[C#] Cannot add items to dataviewgrid winforms - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Cannot add items to dataviewgrid winforms (/showthread.php?tid=5861934)



Cannot add items to dataviewgrid winforms - Rhinemann - Jul 14, 2018

I've been searching for hours for an answer to my problem but I really can't find one. So basically I can add to dataviewgrid from any UI based function (label clicks, main, button clicks) but cannot add data if i create the function myself, I've tried every method I can find and they simply do not work. I even loaded up some of my older projects (i stopped using winforms) and I still couldn't figure it out.

My problem: Cannot add data to dataviewgrid from any function that isn't specifically winforms related

Please, someone put me out of my misery.


RE: Cannot add items to dataviewgrid winforms - TRH4CK - Jul 14, 2018

It's because you are not on the same Thread.

What you can do is invoke it using the invoke method of the datagridview.

So basically, to add you would use something like this:
Code
dataGridView1.Invoke((MethodInvoker)delegate {
   dataGridView1.Rows.Add(new string[] { "Content 1", "Content 1"});
   dataGridView1.Rows.Add(new string[] { "Content 1", "Content 1"});
});



RE: Cannot add items to dataviewgrid winforms - Rhinemann - Jul 14, 2018

(Jul 14, 2018 07:06 PM)TRH4CK Wrote:  It's because you are not on the same Thread.

What you can do is invoke it using the invoke method of the datagridview.

So basically, to add you would use something like this:
Code
dataGridView1.Invoke((MethodInvoker)delegate {
   dataGridView1.Rows.Add(new string[] { "Content 1", "Content 1"});
   dataGridView1.Rows.Add(new string[] { "Content 1", "Content 1"});
});

I also already tried this, I was given this error

Code
'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.'

I'm aware of what it means but It's the very last block of code called, I'm not quite sure what else to do I've tired using IsHandleCreated and had no luck


RE: Cannot add items to dataviewgrid winforms - Rhinemann - Jul 21, 2018

Anyone got any ideas? I still haven't solved this.


RE: Cannot add items to dataviewgrid winforms - Washi - Jul 21, 2018

I don't think we can help you much further without some source code that you are currently using and possibly an error message that you are getting :).


RE: Cannot add items to dataviewgrid winforms - Rhinemann - Jul 21, 2018

(Jul 21, 2018 04:26 AM)Washi Wrote:  I don't think we can help you much further without some source code that you are currently using and possibly an error message that you are getting :).

That's the thing, i've tried tons of ways of adding to the grid. At the moment I moved to using a datasource from my results. The error message is above but it's:
Code
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

But I don't understand why the handle isn't created, I've even tried to force create it.


Solved by modifying program.cs to include a work around for accessing form1.