【问题标题】:Updating datagrid from SQL server - update process not working从 SQL 服务器更新数据网格 - 更新过程不起作用
【发布时间】:2012-12-12 08:01:59
【问题描述】:

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

问题是当我想更新数据网格并再次从 SQL 服务器同步/重新加载表时。

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

当我第一次启动应用程序时,我正在调用此函数并且它正在运行,但之后如果我再次使用该处理程序调用它,我会遇到一些错误。

跨线程操作无效:控制''从线程访问 除了创建它的线程之外。

函数外(全局变量):

SqlConnection sConDataGrid; 
DataSet dbDataSet;
SqlDataAdapter da;
BindingSource dbBind;
string sqlCommand;

EDIT - I forgot to add :
DataGridView _dbView;

这是我的代码:

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() 函数,但它没有刷新数据网格。 我试图在再次填充之前清除数据集,但它使程序崩溃。 我试着做_dbView.Refresh(),但效果不佳..

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

我尝试制作一个计时器,将其置于启用状态,并在函数结束时将其调用为stop(),并在接收到的串行中将其设置为start(),但我在SerialPort 处理程序中所做的一切都是就像“在”程序之外。它不会启动计时器。

【问题讨论】:

  • 我假设 _dbView 是您的 DataGrid。那正确吗?您是否在设置 DataSource 的行之后尝试过 _dbView.DataBind()?
  • @Melanie - 是的,这是我的 DatGrid,我之前没有尝试过,但我没有检查,没有这样的功能:/
  • 跨线程操作无效:控件''从创建它的线程以外的线程访问。 什么控件以及发生此错误的位置?看起来您正在尝试从与创建的内容不同的线程访问某些内容。可能是dataGridView。解决方案可能是通过 Invoke 访问它,但除非您提供有关错误的更多详细信息,否则无法告诉您更多信息
  • 发生此错误时,这是​​我得到的错误,带有''。调试器指向code _dbView.DataSource = dbBind; code 线。我有一个附加到 COM 端口的“已接收”事件处理程序,当我将数据发送到 COM 端口时,我正在根据字符串添加/更改数据库,然后再次调用此函数。
  • 我认为问题在于您在主线程上创建了 DataGrid。但是响应 COM 端口事件而运行的事件处理线程正在尝试修改 GUI。也许事件处理线程可以向主线程发送消息以刷新 DataGrid。

标签: c# sql winforms data-binding datagrid


【解决方案1】:

已解决 读完后:http://msdn.microsoft.com/en-us/library/ms171728%28VS.80%29.aspx 我尝试使用 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();
                    }
                }
            }
        }

【讨论】:

    【解决方案2】:

    试试这个:

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

    那么您所要做的就是在需要“刷新”网格时调用此方法。

    【讨论】:

    • 嘿,我不能使用 .DataBind(); 。它没有显示这个函数,如果我手动编写它 - 我会得到一个编译错误。
    • msdn.microsoft.com/en-us/library/…没有这种方法DataBind()
    • 您必须将这两个程序集引用添加到您的解决方案 System.Data 和 System.Data.SqlClient - 然后它将起作用。
    【解决方案3】:

    我认为有很多原因。 一:你的缓存不清楚 二:你的电脑运行缓慢 thd :您的刷新并不是真正的刷新。 四:可能有时间延迟

    首先 您在数据库中执行“选择 sql”,确保您确实提交 二 你可以写一个背景词让你的程序时间刷新。或制作一个按钮来刷新。 thd 让您的操作重新开始。

    【讨论】:

    • 我已经等了几分钟希望它会更新,但它没有。数据库现在很少更新(在我的测试中),我可以控制 COM 端口数据频率。我想取消每 x 秒的后台刷新,因为我认为这是一种资源浪费。我确切地知道数据库何时发生更改,因此我确切地知道何时更新 DataGrid。
    猜你喜欢
    • 2020-10-27
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多