如下代码,为什么修改时不能更新呢?dataGridView.AllowUserToAddRows=false;
dataGridView.ReadOnly=true;
        SqlDataAdapter da=null ;
        SqlCommandBuilder cb=null ;
        SqlCommand sqlcom=null ;
        SqlConnection sqlcon = null;
        DataTable dt = null;
  private void showtm() {
            if (sqlcon.State == ConnectionState.Open)
            {
                string comstr = string.Empty;
                switch (tabControl_tmlist.SelectedIndex)
                {
                    case 0:
                        comstr = "SELECT * FROM TIANKONGTI";
                        break;
                    case 1:
                        comstr = "SELECT * FROM PANDUANTI";
                        break;
                    case 2:
                        comstr = "SELECT * FROM XUANZETI";
                        break;
                    case 3:
                        comstr = "SELECT * FROM WENDATI";
                        break;
                }
                da = new SqlDataAdapter(comstr,sqlcon);
                cb = new SqlCommandBuilder(da);
                dt = new DataTable();                      
                da.Fill(dt);
                dataGridView_tmlist.DataSource = dt;             
               else { //未连接
            }
        }
//删除,可更新
 private void StripMenuItem_tkgl_del_Click(object sender, EventArgs e)
        {
            dataGridView_tmlist.Rows.Remove(dataGridView_tmlist.SelectedRows[0]);
            da.Update(dt);
        }
//修改为什么不能更新?
 private void StripMenuItem_tkgl_modfy_Click(object sender, EventArgs e)
        {
            Form_Modfy modfy = new Form_Modfy();
            modfy.index = tabControl_tmlist.SelectedIndex;
            modfy.selectrow = dataGridView_tmlist.SelectedRows[0];
            if (modfy.ShowDialog() == DialogResult.OK) {
                for (int i = 1; i < dataGridView_tmlist.SelectedRows[0].Cells.Count; i++) 
                {          
                    dataGridView_tmlist.SelectedRows[0].Cells[i].Value = modfy.selectrow.Cells[i].Value;
                }
                da.Update(dt);
            }
           
        }
DataGridView

解决方案 »

  1.   

    dataGridView.ReadOnly=false; 就可以了,但我又不想让用户更改怎么办呢?加个
    dataGridView.ReadOnly=false;
    da.Update(dt);
    dataGridView.ReadOnly=true;
    ???????????
      

  2.   

    dataGridView.ReadOnly=false;//你就设置为false好了,反正不能真正修改
      

  3.   

    dataGridView.ReadOnly=false;也不行啊,刚才我也不知道动哪了就行了,刚试了,不行
      

  4.   

    dataGridView_tmlist.CommitEdit 试试呢?
      

  5.   

    帮忙解答
    http://bbs.csdn.net/topics/390510770