public list()
        {
            InitializeComponent();            DataSet ds = dataconn.getlist("select top " + pagesize + " content,zz from thread t,user u where u.zzid=t.zzid order by id desc");
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            this.dataGridView1.Columns[0].HeaderText = "内容";
            this.dataGridView1.Columns[1].HeaderText = "作者";            DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
            btn1.HeaderText = "";
            btn1.Text = "编辑";
            btn1.Name = "edit";
            btn1.Width = 40;
            btn1.UseColumnTextForButtonValue = true;
            this.dataGridView1.Columns.Add(btn1);            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.HeaderText = "";
            btn.Text = "删除";
            btn.Name = "del";
            btn.SortMode = DataGridViewColumnSortMode.NotSortable;
            btn.Width = 40;
            btn.UseColumnTextForButtonValue = true;
            this.dataGridView1.Columns.Add(btn);            this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(DrawRowIndex);
        }private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "del")
            {
                int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                dataconn.update("delete from thread where id = " + id);
                MessageBox.Show("删除成功!", "提示");
                page = 2;
                getpagecount();
                kk(0);
            }            if (dataGridView1.Columns[e.ColumnIndex].Name == "edit")
            {
                editit editfrm = new editit(int.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()), this);
                editfrm.ShowDialog();
            }
        }我在一个FORM窗体中,拖入一个name为dataGridView1的dataGridView,然后绑定数据,在里面增加了两个按钮的功能,一个是编辑,还有一个是删除,不管是我点删除,还是编辑,都提示错误,形式上,好像是没有获取到对应的 ID,请问正确的获取方式是?谢谢

解决方案 »

  1.   

    select top " + pagesize + " content,zz from thread t,user u where u.zzid=t.zzid order by id desc
    这个sql语句有ID吗? 需要添加绑定ID
    dataGridView1.Rows[e.RowIndex].Cells[2].Value这个是什么,它就是编辑列,怎么获取ID
      

  2.   

    问:你只给dataGridView绑定了两列数据,那么你获取的id是dataGridView1里面的id吗?
     我感觉这样做可能会好些:    你再添加一列数据库中的id,把它了隐藏起来,然后你再获取它,用这个id做为删除条件
      

  3.   

    dataGridView1.Rows[e.RowIndex].Cells[2].Value
    这个值是第三个单元格绑定的值。你在绑定数据源,查看         DataSet ds = dataconn.getlist("select top " + pagesize + " content,zz from thread t,user u where u.zzid=t.zzid order by id desc"); 
             this.dataGridView1.DataSource = ds.Tables[0].DefaultView; 
    前台绑定的第三列 是否是绑定id列。
      

  4.   

    奇怪了DataSet ds = dataconn.getlist("select top " + pagesize + " id,content,zz from thread t,user u where u.zzid=t.zzid order by id desc"); 
             this.dataGridView1.DataSource = ds.Tables[0].DefaultView; 
    我写成上面的格式,用
    dataGridView1.Rows[e.RowIndex].Cells[2].Value 能获取到id,反而用dataGridView1.Rows[e.RowIndex].Cells[0].Value ,获取不到id,提示错误
      

  5.   

    说明一下,我是按照SQL语句绑定的数据this.dataGridView1.Columns[0].HeaderText = "序号"; 
    this.dataGridView1.Columns[1].HeaderText = "内容"; 
    this.dataGridView1.Columns[2].HeaderText = "作者";
      

  6.   

    dataGridView1.Rows[e.RowIndex].Cells[2].Value 能获取到id,反而用dataGridView1.Rows[e.RowIndex].Cells[0].Value ,获取不到id,提示错误 你可以设断点看看 dataGridView1.Rows[e.RowIndex].Cells[0].Value  (第一个单元格,也就是第一列绑定的是什么值 那样更容易理解 )string str = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();看看str 是什么值