-1

I have a problem with my data grid view, after i add a row the program freeze, this is my code:

private void ActivityLogAddRow(bool status, string desc, AVType avt)
    {
            DataGridViewRow row = new DataGridViewRow();
            DataGridViewCell imageCell = new DataGridViewImageCell();
            if (status == true)
                imageCell.Value = Properties.Resources.success;
            else
                imageCell.Value = Properties.Resources.failure;
            DataGridViewCell timeCell = new DataGridViewTextBoxCell();
            timeCell.Value = DateTime.Now;
            DataGridViewCell typeCell = new DataGridViewTextBoxCell();
            typeCell.Value = avt.ToString();
            DataGridViewCell descCell = new DataGridViewTextBoxCell();
            descCell.Value = desc;
            row.Cells.Add(imageCell);
            row.Cells.Add(timeCell);
            row.Cells.Add(typeCell);
            row.Cells.Add(descCell);
            dataGridView1.Rows.Add(row);
        }

I can see the row added to the data grid view and than its(the program) stops, any body have a clue ?

p.s its takes a little bit too long to add the row(like couple of sec')

Update:

I get exception about two threads accessing the Datagridview, how can i fix it ? mutex ?

TomTom
  • 61,059
  • 10
  • 88
  • 148
Troncho
  • 77
  • 2
  • 10
  • When is this method being called? – LarsTech Mar 20 '14 at 21:08
  • I add to the grid view a row every time something Happens, so its during process my projects does, its important when i called this method ? – Troncho Mar 22 '14 at 13:32
  • "is the Exception i get: Something about two threads accessing the gridView" - you mean have never heard of copy paste? YOu mean you are not capable of writing down an exception text word by word? I really hate to be snippy, but "something aobut". Well, "something is wrong about your code, fix it" - i hope that helped. – TomTom Mar 22 '14 at 13:58
  • TomTom i tried to put a picture on and somebody(i guess its you vote me off so now i cant, thank you very much). the problem is that i got two threads access the gridview and it is not safe, so i ask what is the best method to go around it. if you doesn't have something meaningful to say don't say! – Troncho Mar 22 '14 at 14:01
  • @RonKah I don't care abut pictue. I am shocked you consider asking for help and not even bother to write down the exception description. Does your computer not have copy/paste for text? what do you run? Mine has it, and had it for years. – TomTom Mar 22 '14 at 14:04
  • @TomTom Open your ears and listen if you want an answer, or just writing stuff and pissing people off. I use the snipping tool to upload the exception(with picture) but you vote me off before i add the chance to update agian so now i doesn't have points to upload the picture, Thank you for that TomTom. Stop being so hateful and understand what i'm saying for the last time. – Troncho Mar 22 '14 at 14:12
  • @RonKah Your point is well taken - by asking a question that people in school would get reprimanded for..... you lost the right to post a picture. Go figure whose fault it is. Now, you could also try to understand the answer I provided, which actually contains the solution for you. Not as a simple "hey, write me me code slave" copy paste issue, as I am not your slave, but as something that points you to the right documentation. I suggest you clean up your question. And seriously, noone wants a picture where simple text copy/paste is better. Except you. CLean up the question please. – TomTom Mar 22 '14 at 14:15
  • You need to document your "two threads" that you are using. Are you using a BackgroundWorker? Are you calling this method in the DoWork method? That was why I was asking "when are you calling that method?" The code you posted by itself can't freeze your program, so that's why no one can really help you at the moment. We don't know enough. Also, as mentioned, we need to see the exception message. – LarsTech Mar 22 '14 at 15:53

2 Answers2

0

What if you try changing the way of adding the row to the following:

 DataTable table = new DataTable();
    table.Columns.Add("Img",typeof(System.Drawing.Image));
    table.Columns.Add("Time");
    table.Columns.Add("Type");
    table.Columns.Add("Desc");
    DataRow dr = table.NewRow();
    if (status == true)
           dr["Img"]  = Properties.Resources.success;
        else
           dr["Img"]  = Properties.Resources.failure;
    dr["Time"] = DateTime.Now;
    dr["Type"] = avt.ToString();
    dr["Desc"] = desc;
    table.Rows.Add(dr);

datagridview1.DataSource = table;
Laila
  • 417
  • 1
  • 3
  • 14
0

ANY UI control in windows follows a STA threading model. Single Threaded Apartment. Any control is owned by the thread that created it and ONLY this thread is allowed to update it. This goes back to ActiveX times - and as any windows UI control has to be ActiveX compatible (otherwise stuff like standard file dialogs would not work) it is in place. It also makes any UI work a lot easier as you can be sure that while a UI operation takes place no other thread will update the UI - nice and synchronized.

This is why your main thread in a C# application is per default marked with STAThread. More info on that is at Why do all Winforms programs require the [STAThread] attribute?

So, your background thread is not allowed to make changes directly.

You CAN, though, use the INVOKE method to execute a block (of code) In the UI Thread.

For this I suggest you read

http://www.codeproject.com/Articles/2083/The-key-to-multi-threaded-Windows-Forms-UI-interac

which explains how to do it - or use the documentation and read about the Invoke method on the UI controls / window. A call to Invoke executes the containing code block in the UI Thread and that one can then update the UI.

So, backgroundworker -> get data -> invoke into UI to update data.

Community
  • 1
  • 1
TomTom
  • 61,059
  • 10
  • 88
  • 148