【问题标题】:DataGridView not updating after an Insert from TableAdapter从 TableAdapter 插入后 DataGridView 未更新
【发布时间】:2015-10-30 15:28:22
【问题描述】:

在我的 form1 中,我正在插入

tableAdapterManager.BatchTableAdapter.Insert(Int32.Parse(Maxorder) + 10, DateTime.Now, DateTime.Now, Int32.Parse(txtData0.Value.ToString()), 1);

所以它将参数添加到我的 LocalDb 中,然后我转到 form2,它应该在 dataGridView 中显示结果。此 DataGridView 与 DataSource 一起成为 View。仅当我重新启动应用程序时,视图才显示“插入”参数。我试过了:

v_BatchTableAdapter.Fill(dbSahara.v_Batch);

但我只在打开应用程序之前插入数据。

如果有人可以帮助我...在此先感谢。

【问题讨论】:

    标签: datagridview insert refresh localdb tableadapter


    【解决方案1】:

    我这样做“修复”了它:

     private void GetData(string selectCommand)
        {
            try
            {
                // Specify a connection string. Replace the given value with a 
                // valid connection string for a Northwind SQL Server sample
                // database accessible to your system.
                String connectionString =
                    "Integrated Security=SSPI;Persist Security Info=False;" +
                    "Initial Catalog=Northwind;Data Source=localhost";
    
                // Create a new data adapter based on the specified query.
                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
    
                // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
    
                // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;
    
                // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns( 
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }
    

    字体:https://msdn.microsoft.com/es-es/library/vstudio/fbk67b6z(v=vs.100).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 2015-06-23
      • 2013-09-04
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多