C# DataGridView编辑
我有一个windows窗体项目C# 我添加了一个DataGridView,并向数据集添加了一个绑定以及复选框 添加 编辑 删除 现在我可以编辑单元格,但编辑后如何保存C# DataGridView编辑,c#,datagridview,edit,C#,Datagridview,Edit,我有一个windows窗体项目C# 我添加了一个DataGridView,并向数据集添加了一个绑定以及复选框 添加 编辑 删除 现在我可以编辑单元格,但编辑后如何保存 protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { string id=// To find key of current row. e.g: myGridView.Dat
protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id=// To find key of current row. e.g: myGridView.DataKeys[e.RowIndex].Value.ToString();
string text=//To find the text value from the current row.
conn.Open();
SqlCommand UpdateCmd = new SqlCommand(" your query");
UpdateCmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1; //After updating, please set EditIndex and re-bind the GridView.
BindGridView();
}
您可以通过以下链接检查完整的功能:
希望有帮助。试试这个:
dgv.CellBeginEdit += dgv_CellBeginEdit;
dgv.CellValidating += dgv_CellValidating;
dgv.CellEndEdit += dgv_CellEndEdit;
private void dgv_CellBeginEdit(Object sender, DataGridViewCellCancelEventArgs e)
{
//Here we save a current value of cell to some variable, that later we can compare with a new value
//For example using of dgv.Tag property
if(e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
this.dgv.Tag = this.dgv.CurrentCell.Value;
//Or cast sender to DataGridView variable-> than this handler can be used in another datagridview
}
}
private void dgv_CellValidating(Object sender, DataGridViewCellValidatingEventArgs e)
{
//Here you can add all kind of checks for new value
//For exapmle simple compare with old value and check for be more than 0
if(this.dgv.Tag = this.dgv.CurrentCell.Value)
e.Cancel = true; //Cancel changes of current cell
//For example used Integer check
int32 iTemp;
if (Int32.TryParse(this.dgv.CurrentCell.Value, iTemp) = True && iTemp > 0)
{
//value is ok
}
else
{
e.Cancel = True;
}
}
Private Sub dgvtest1_CellEndEdit(Object sender, DataGridViewCellEventArgs e)
{
//Because CellEndEdit event occurs after CellValidating event(if not cancelled)
//Here you can update new value to database
}
查看以上帖子了解更多详细信息。我无法帮助您,因为我不是C#人,但我建议您添加更多详细信息,如果可能的话,添加您的试用代码的一部分,以便成员能够快速理解和帮助您