当选中启用编辑,启用删除后,对datagridview中数据记录的改变,只会在该控件中更新,不会自动更新数据库中的记录,下次打开时,显示的记录没变化,这和web中的GridView很不一样。
  请问:怎样实现当用户改变DataGridView的记录时,能同时更新数据库中的数据!另外,DataGridView有没有验证用户输入的数据的合法性的功能?
                      请给出完整的代码,代码越长越好!

解决方案 »

  1.   

    不用写代码...
    右上角有个小箭头,点击它,然后选择数据源 按提示步骤配置你的数据源 配置好了后 DataGridView会显示你配好的那张表,然后修改数据,它自己会更新数据库的  还有一些细节我没说  想必楼主这么聪明  毋庸赘言了吧
      

  2.   

    1。对DTAAGRIDVIEW绑定的DataTable
    DbDataAdapter.Update (DataTable) 2.在CellValidating中验证,下边的例子是验证是否输入的是整数
     private void DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {            if (e.ColumnIndex != 1 && e.ColumnIndex != 2) { return; }
                int newInteger = 0;
                if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0)
                {
                     dgBase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
                     e.Cancel = true;            } 
               
            }