简体   繁体   中英

Gridview not adding new row in C#

I am using Microsoft Visual Studio Professional 2017 C#. This is a weird one. I am trying to add a row to my Gridview table. However, the row only shows up when I switch to a new tab and then return to that tab. So I have 5 tabs. And if I switch to another tab and then come back to that same tab then, I see my new row. I tried to do this :

public void UpdateGrid(int number)
{
    try
    {
        if (InvokeRequired)
        {
            BeginInvoke(new Action(() =>
            {
                if (number == 1)
                    dataGridView1.Update();
                else
                    dataGridView2.Update();
            }));

            return;
        }
        
        if (number == 1)
            dataGridView1.Update();
        else
            dataGridView2.Update();
    }
    catch (Exception y) {}
}

but it still does the same thing. The new row will not show up until I switch to a new tab and return to that tab. I am using threading and that is what I have my update like that.

here is my code:

void add_To_table(string site, string X, string Y, float Ts625, float Tp625, float CR625, float Ts1550, float Tp1550, float CR1550, string Xum, string Yum, int tablePick )
{
    try
    {        
        if (tablePick == 1)
        {
            table.Rows.Add(site, X, Y, Ts625, Tp625, CR625, Ts1550, Tp1550, CR1550, Xum, Yum);
            dataGridView1.DataSource = table;
        }
        else
        {
            table2.Rows.Add(site, X, Y, Ts625, Tp625, CR625, Ts1550, Tp1550, CR1550, Xum, Yum);
            dataGridView2.DataSource = table2;
        }
    }
    catch(Exception err)
    {
        MessageBox.Show(err.Message);
    }
}

I am not 100 percent sure it may be solution but have you ever write Update() command outside the try{}

void add_To_table(string site, string X, string Y, float Ts625, float Tp625, 
float CR625, float Ts1550, float Tp1550, float CR1550, string Xum, string Yum, 
int tablePick )
{
try
{        
    if (tablePick == 1)
    {
        table.Rows.Add(site, X, Y, Ts625, Tp625, CR625, Ts1550, Tp1550, CR1550, Xum, Yum);
        dataGridView1.DataSource = table;
    }
    else
    {
        table2.Rows.Add(site, X, Y, Ts625, Tp625, CR625, Ts1550, Tp1550, CR1550, Xum, Yum);
        dataGridView2.DataSource = table2;
    }
}
catch(Exception err)
{
    MessageBox.Show(err.Message);
}
dataGridView1.Update();
dataGridView2.Update();
}

I will be very happy if you post result after you tried

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM