繁体   English   中英

Gridview 未在 C# 中添加新行

[英]Gridview not adding new row in C#

我正在使用 Microsoft Visual Studio Professional 2017 C#。 这是一个奇怪的。 我正在尝试向我的 Gridview 表中添加一行。 但是,该行仅在我切换到新选项卡然后返回到该选项卡时才会显示。 所以我有 5 个标签。 如果我切换到另一个选项卡,然后又回到同一个选项卡,我会看到我的新行。 我试图这样做:

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) {}
}

但它仍然做同样的事情。 在我切换到新选项卡并返回到该选项卡之前,不会显示新行。 我正在使用线程,这就是我的更新。

这是我的代码:

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);
    }
}

我不是 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();
}

如果您在尝试后发布结果,我会很高兴

暂无
暂无

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

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