datagridviewg更新记录出错
private void button1_Click_1(object sender, EventArgs e){
string sql = "update text1 set bh='" + textBox1.Text + "'mc='" + textBox2.Text + "'bz='" + textBox3.Text + "'where bh=this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString()";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=LUKE\\SQLEXPRESS;uid=sa;pwd=sa;database=luke";
conn.Open();
DataTable dt = new DataTable("resouce");
dt.Clear();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = command;
ad.Fill(dt);
dt.Clear();
MessageBox.Show("该学生已被删除!", "提示");
SqlDataAdapter adp = new SqlDataAdapter("select * from text1", conn);
adp.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
private void Form3_Load(object sender, EventArgs e)
{
dataGridView1.ReadOnly = true;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=LUKE\\SQLEXPRESS;uid=sa;pwd=sa;database=luke";
conn.Open();
DataTable dt = new DataTable("resouce");
dt.Clear();
SqlDataAdapter adp = new SqlDataAdapter("select * from text1", conn);
adp.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
textBox1.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
}