Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I have a Datagridview that receives external data and I added 3 columns independent of the external data

My datagridview refreshes every 3 seconds and during the refresh the table flickers (that is to say that the cells shrink and return to their original size) and also the content of the 3 independent columns (those added) disappear and reappear.

I have this problem with this added code (see below which corresponds to an added column), if I delete this piece of code the problem disappears:

C#
var id = row.Cells[0].Value.ToString();
var nickNameTask = Task.Run<string>(async() => await this.GetTraderName(id));
nickNameTask.Wait();
string nickName = nickNameTask.Result;
row.Cells[1].Value =nickName;


Do you know where the problem can come from and how to correct it?

What I have tried:

Here is the code:

<pre lang="C#"><pre> private void dtgvPositions_DataBindingComplete(
          object sender,
          DataGridViewBindingCompleteEventArgs e)
        {
            List<string> lstUID = this.ReplaceDownTheLine(this.txtUIDs.Text.Trim());
            Dictionary<string, bool> lstUIDColor = new Dictionary<string, bool>();
            for (int index = 0; index < lstUID.Count; ++index)
                lstUIDColor.Add(lstUID[index], index % 2 == 0);
            this.dtgvPositions.Invoke((Action)(() =>
            {
                foreach (string str in lstUID)
                {
                    foreach (DataGridViewRow row in (IEnumerable)this.dtgvPositions.Rows)
                    {
                        string key  = row.Cells[0].Value.ToString();
                        Decimal num = 0M;
                        Decimal num1 = 0M;
                        Decimal num2 = 0M;
                        Decimal num3 = 0M;
                        try
                        {
                            num  = Decimal.Parse(row.Cells[9].Value.ToString());
                            num1 = Decimal.Parse(row.Cells[5].Value.ToString());
                            num2 = Decimal.Parse(row.Cells[7].Value.ToString());
                            num3 = Decimal.Parse(row.Cells[3].Value.ToString());


                            var id = row.Cells[0].Value.ToString();
                            var nickNameTask = Task.Run<string>(async() => await this.GetTraderName(id));
                            nickNameTask.Wait();
                            string nickName = nickNameTask.Result;
                            row.Cells[1].Value =nickName;

                            bool flag;
                            lstUIDColor.TryGetValue(key, out flag);
                            row.DefaultCellStyle.BackColor = Color.FromArgb(24, 26, 32);
                            row.Cells[1].Selected = false; 
                            row.Cells[1].Style.ForeColor = flag ? Color.LightSkyBlue : Color.LightPink; 
                            row.Cells[2].Value = num1 >= 0M ? "Long" : "Short";
                            row.Cells[2].Style.ForeColor = num1 >= 0M ? Color.FromArgb(22, 181, 93) : Color.FromArgb(194, 52, 54);
                            row.Cells[6].Value = (num1 * num2) / num3; 
                            row.Cells[9].Style.ForeColor = num >= 0M ? Color.FromArgb(22, 181, 93) : Color.FromArgb(194, 52, 54);
                            row.Cells[10].Style.ForeColor = num >= 0M ? Color.FromArgb(22, 181, 93) : Color.FromArgb(194, 52, 54);
                        }
                        catch
                        {
                        }

                    }
                }
            }));
        }
Posted
Updated 15-May-23 14:16pm

1 solution

 
Share this answer
 
Comments
QuickSilvain 2023 17-May-23 4:51am    
Thanks, you mean I just have to set VirtualMode = true? I tried that but no change..

I read the links but it's complicated for me to set up.. Thank you again

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