首先我应该说我看到了这个链接:
我确实喜欢这样:(我在 Form1 中有 datagridview) Form1:
public void FillDataGridView(DataTable dt1)
{
bindingSource1.DataSource = dt1;
bindingSource1.ResetBindings(false);
dataGridView1.DataSource = bindingSource1;
//here i checked number of rows of dt1 and it shows the correct value
}
表格2:
SqlConnection cnn = new SqlConnection(@"My connection string");
private Form1 Handled_frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
Handled_frm1 = frm1;
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
dt.Clear();
using (SqlCommand cmd =cnn.CreateCommand())
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;
cmd.CommandText = "spSearchCustomerByName";
SqlParameter inparam1 = cmd.Parameters.AddWithValue("@name", textbox1.Text);
inparam1.Direction = ParameterDirection.Input;
dap.SelectCommand = cmd;
dap.Fill(dt);
Handled_frm1.FillDataGridView(dt);
}
但是 Datagridview 的值并没有改变!
编辑:
我想测试是否可以清除数据网格视图,所以我像这样更改了 FillDataGridView:
public void FillDataGridView(DataTable dt1)
{
dt.Clear();
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
但它并没有清除datagridview1!!!