简体   繁体   English

Gridview 未在 C# 中添加新行

[英]Gridview not adding new row in C#

I am using Microsoft Visual Studio Professional 2017 C#.我正在使用 Microsoft Visual Studio Professional 2017 C#。 This is a weird one.这是一个奇怪的。 I am trying to add a row to my Gridview table.我正在尝试向我的 Gridview 表中添加一行。 However, the row only shows up when I switch to a new tab and then return to that tab.但是,该行仅在我切换到新选项卡然后返回到该选项卡时才会显示。 So I have 5 tabs.所以我有 5 个标签。 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{}我不是 100% 确定它可能是解决方案,但您是否曾经在 try{} 之外编写过 Update() 命令

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如果您在尝试后发布结果,我会很高兴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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