从TableAdapter

时间:2015-10-30 15:28:22

标签: datagridview insert refresh localdb tableadapter

在我的form1中,我插入

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

所以它添加了我的LocalDb参数,但后来我去了form2,它应该在dataGridView中显示结果。此DataGridView使用DataSource到View。该视图从不显示"插入"参数,仅当我重新启动Application时。我尝试过:

v_BatchTableAdapter.Fill(dbSahara.v_Batch);

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

如果有人可以帮助我......提前谢谢。

1 个答案:

答案 0 :(得分:0)

我“修复”它这样做:

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

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