Results 1 to 5 of 5

Thread: Why my DataGridView shimmers (column shrink and resume their sizes)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2023
    Posts
    31

    Why my DataGridView shimmers (column shrink and resume their sizes)

    Hi all,

    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 refreshment the table sparkles (i.e. the cells shrink and return to their original sizes) and also the content of the 3 independent columns (those added) disappears and reappears.

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

    Code:
    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 may come from and how to fix it?

    Here is the code:

    Code:
     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
                            {
                            }
    
                        }
                    }
                }));
            }
    Last edited by jmcilhinney; May 15th, 2023 at 08:52 PM. Reason: Translated to English

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why my DataGridView shimmers (column shrink and resume their sizes)

    Your thread title and post have been translated to English. We only accept posts in English on this site. Any further threads not in English will be closed.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2023
    Posts
    31

    Re: Why my DataGridView shimmers (column shrink and resume their sizes)

    Sorry and thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why my DataGridView shimmers (column shrink and resume their sizes)

    The problem is that your event handler is executed on the UI thread and you're freezing that thread every time you call Wait. You'd be better off getting all the data you need first, initiating a thread or task to do that work and marshalling back to the UI thread as required along the way or once at the end. That way, you only monopolise the UI thread to make changes to the UI, not to do a lot of background work. I'd have to look more closely at the code to provide more specific advice and I don;t have time for that right now, but that's the way you need to go.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2023
    Posts
    31

    Re: Why my DataGridView shimmers (column shrink and resume their sizes)

    Quote Originally Posted by jmcilhinney View Post
    The problem is that your event handler is executed on the UI thread and you're freezing that thread every time you call Wait. You'd be better off getting all the data you need first, initiating a thread or task to do that work and marshalling back to the UI thread as required along the way or once at the end. That way, you only monopolise the UI thread to make changes to the UI, not to do a lot of background work. I'd have to look more closely at the code to provide more specific advice and I don;t have time for that right now, but that's the way you need to go.
    Hi, there,

    Really sorry for the long delay as I had a problem with my PC

    My problem is still there, what's a thread?

    Do you know how to get all the data first?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width