注册 登录
编程论坛 C# 论坛

datagridviewg更新记录出错

lukebc 发布于 2016-08-31 14:55, 2466 次点击
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();
        }

只有本站会员才能查看附件,请 登录
2 回复
#2
沉迷敲代码2016-08-31 20:27
回复 楼主 lukebc
第3行ma没有,号
#3
沉迷敲代码2016-09-01 11:13
string sql = "update text1 set bh='" + textBox1.Text + "',mc='" + textBox2.Text + "',bz='" + textBox3.Text + "'  where bh=this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString()";
改的时候每个要,号分开   where条件前加空格
1