C# 正在从SQL server更新datagrid-更新进程不工作

C# 正在从SQL server更新datagrid-更新进程不工作,c#,sql,winforms,data-binding,datagrid,C#,Sql,Winforms,Data Binding,Datagrid,我有一个SQL server,我想在我的应用程序的主窗体中显示它。 我遵循了一些指南,并成功地尝试在dataGrid和SQL server表之间进行链接 问题是,当我想更新datagrid并再次从SQL server同步/重新加载表时 我有一个事件处理程序,每当我在数据库中添加/编辑行时,它都会调用一个updateadadrid()函数 当我第一次启动应用程序时,我正在调用这个函数,它正在工作,但是在那之后,如果我用那个处理程序再次调用它,我会得到一些错误 跨线程操作无效:从线程访问控件“” 而

我有一个SQL server,我想在我的应用程序的主窗体中显示它。 我遵循了一些指南,并成功地尝试在dataGrid和SQL server表之间进行链接

问题是,当我想更新datagrid并再次从SQL server同步/重新加载表时

我有一个事件处理程序,每当我在数据库中添加/编辑行时,它都会调用一个
updateadadrid()
函数

当我第一次启动应用程序时,我正在调用这个函数,它正在工作,但是在那之后,如果我用那个处理程序再次调用它,我会得到一些错误

跨线程操作无效:从线程访问控件“” 而不是创建它的线程

函数外部(全局变量):

这是我的代码:

private void updateDataGrid()
{
        using (sConDataGrid = new SqlConnection("Data Source=" + SettingsForm.getAddress + ";Initial Catalog=" + SettingsForm.getDatabase + ";Integrated Security=False;User Id=" + SettingsForm.getUser + ";Password=" + SettingsForm.getPassword + ";Connect Timeout=0;"))
        {
            sConDataGrid.Open();
            sqlCommand = "select top 200 * FROM cstPackages order by _dateTime desc"; //reading the db from end to start   
            using (da = new SqlDataAdapter(sqlCommand, sConDataGrid))
            {
                da.Fill(dbDataSet, "cstPackages");
                    dbBind = new BindingSource(dbDataSet, "cstPackages");
                    _dbView.DataSource = dbBind;
                sConDataGrid.Close();
            }
        }
}
我尝试使用它,在我第一次调用该函数后只调用
da.Fill()
函数,但它没有刷新数据网格。 我试图在再次填充数据集之前清除数据集,但它使程序崩溃。 我试着只做
\u dbView.Refresh()
,但效果不太好

编辑 我不明白,为什么如果我做了一个按钮,我把“点击”事件处理程序,我从这个事件处理程序调用我的函数-它工作了! 如果我从
SerialPort
dataReceived
事件处理程序调用此函数,我会收到此错误!有什么区别? 我想可能是我创建事件处理程序的方式不正确,所以我只是从设计器中拖动了一个
SerialPort
控件,然后双击事件,同样的情况也发生了

我试图创建一个计时器,将其启用,并在函数末尾将其调用到
stop()
,然后在串行接收中将其设置为
start()
,但我在
SerialPort
处理程序中所做的一切都像是“在程序之外”。它不会启动计时器。

请尝试以下操作:

     private void fill_grid()
    {

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Your sql command ";

        cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = your_control.Text;

        cmd.CommandType = CommandType.Text;
        cmd.Connection = this.sqlConnection1;
        this.sqlConnection1.Open();


        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adpt.Fill(ds);


        your_grid.DataSource = ds;


        this.sqlConnection1.Close();

        this.your_grid.DataBind();



    }
然后,只要在需要“刷新”网格时调用此方法即可

我想有很多原因。 第一:你的缓存不清晰 你的电脑工作很慢 thd:你的刷新不是真正的刷新。 第四:可能有时间延迟

首先 在数据库中执行“选择sql”操作时,请确保确实提交 2 你可以写一个背景词,让你的pragram时间刷新。或者制作一个按钮来刷新。 thd 让你的电脑重新打开

已解决 读完这篇文章: 我试着使用Invokes,它成功了

new code:
        delegate void SetDataGridCallback();

        private void updateDataGrid()
        {
            if (this._dbView.InvokeRequired)
            {
                SetDataGridCallback d = new SetDataGridCallback(updateDataGrid);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                using (sCon2 = new SqlConnection("Data Source=" + SettingsForm.getAddress + ";Initial Catalog=" + SettingsForm.getDatabase + ";Integrated Security=False;User Id=" + SettingsForm.getUser + ";Password=" + SettingsForm.getPassword + ";Connect Timeout=0;"))
                {
                    sCon2.Open();
                    string sqlCommand = "select top 200 * FROM cstPackages order by _dateTime desc"; //reading the db from end to start   
                    using (da = new SqlDataAdapter(sqlCommand, sCon2))
                    {
                        dbDataSet.Clear();
                        da.Fill(dbDataSet, "cstPackages");
                        BindingSource dbBind = new BindingSource(dbDataSet, "cstPackages");
                        _dbView.DataSource = dbBind;
                        _dbView.Refresh();
                         sCon2.Close();
                    }
                }
            }
        }

我假设dbView是您的数据网格。是这样吗?您在设置数据源的那一行之后尝试过_dbView.DataBind()吗?@Melanie-是的,这是我的DatGrid,我以前没有尝试过,但我没有选中,并且没有这样的函数:/Cross thread operation无效:控件“”是从创建它的线程以外的线程访问的。什么控件以及此错误发生的位置?看起来您正在尝试从不同的线程访问某个内容,而不是创建该内容。可能是dataGridView。解决方案可以是通过Invoke访问它,但不能告诉您更多信息,除非您提供有关错误的更多详细信息。当发生此错误时,这是我得到的错误,带有“”。调试程序指向
code
\u dbView.DataSource=dbBind<代码>代码行。我有一个连接到COM端口的“received”事件处理程序,当我向COM端口获取数据时,我会根据字符串添加/更改数据库,然后再次调用此函数。我认为问题在于您在主线程上创建了DataGrid。但是,作为对COM端口事件的响应而运行的事件处理线程正在尝试修改GUI。也许事件处理线程可以向主线程发送一条消息来刷新DataGrid。它没有显示这个函数,如果我手动编写它,就会出现编译错误。没有这样的方法
DataBind()
您必须将这两个程序集引用添加到您的解决方案System.Data和System.Data.SqlClient-然后它就会工作。我已经等了几分钟,希望它会更新,但它没有。数据库现在很少更新(在我的测试中),我可以控制COM端口数据频率。。我想取消每x秒一次的背景刷新,因为我认为这是浪费资源。我确切地知道数据库何时更改,所以我确切地知道何时更新数据网格。
new code:
        delegate void SetDataGridCallback();

        private void updateDataGrid()
        {
            if (this._dbView.InvokeRequired)
            {
                SetDataGridCallback d = new SetDataGridCallback(updateDataGrid);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                using (sCon2 = new SqlConnection("Data Source=" + SettingsForm.getAddress + ";Initial Catalog=" + SettingsForm.getDatabase + ";Integrated Security=False;User Id=" + SettingsForm.getUser + ";Password=" + SettingsForm.getPassword + ";Connect Timeout=0;"))
                {
                    sCon2.Open();
                    string sqlCommand = "select top 200 * FROM cstPackages order by _dateTime desc"; //reading the db from end to start   
                    using (da = new SqlDataAdapter(sqlCommand, sCon2))
                    {
                        dbDataSet.Clear();
                        da.Fill(dbDataSet, "cstPackages");
                        BindingSource dbBind = new BindingSource(dbDataSet, "cstPackages");
                        _dbView.DataSource = dbBind;
                        _dbView.Refresh();
                         sCon2.Close();
                    }
                }
            }
        }