我有一个用于显示数据的gridview。每当我进行数据更新并按更新按钮时,我的数据都会更新。但是,当我使用文本框从gridview搜索记录然后进行更新时,它不起作用。 这是我的显示数据代码:
SqlDataAdapter sda;
DataTable dt;
SqlCommandBuilder scb;
DataSet ds;
SqlConnection con8 = new SqlConnection(@"Data Source=RAMISHA-PC;Initial Catalog=sales;Integrated Security=True");
public void display_data()
{
try
{
con8.Open();
sda = new SqlDataAdapter("SELECT * FROM category", con8);
ds = new System.Data.DataSet();
dt = new DataTable("product update");
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns["com_id"].ReadOnly = true;
dataGridView1.Columns[0].HeaderText = "Comp Id";
dataGridView1.Columns[1].HeaderText = "Company Name";
dataGridView1.Columns[1].Width = 200;
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[2].HeaderText = "Category Name";
dataGridView1.Columns[2].Width = 200;
con8.Close();
}
catch (Exception)
{
MessageBox.Show("There is some error. Please try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这是我的按钮点击事件更新代码:
private void Button3_Click(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(sda);
sda.Update(dt);
display_data();
}
这是我的文本框搜索代码:
private void TxtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("com_name like '%{0}%' ", txtSearch.Text);
dataGridView1.DataSource = dv.ToTable();
}
}
现在,当我搜索搜索记录时,我希望对其进行更新。但是,当我更新并按下更新按钮时,它不会起作用。请帮我哪里错了?