如何通过Winform实现双击表格单元格直接修改数据的功能?

2026-03-30 14:451阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计626个文字,预计阅读时间需要3分钟。

如何通过Winform实现双击表格单元格直接修改数据的功能?

在表格中,当单元格值更改时,除了第一行外,其他行的更改将执行更新操作。以下是简化后的代码:

csharppublic List CountryList_uplist=new List();

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){ if (e.RowIndex !=-1) { // 更新操作 }}

表格触发事件

如何通过Winform实现双击表格单元格直接修改数据的功能?


public List<Models.CountryList> uplist = new List<Models.CountryList>();

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
return;
}
Upuplist(e.RowIndex);
}

public void Upuplist(int RowIndex)
{
Models.CountryList d = new Models.CountryList();
d.Id = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Id"].Value);
if (uplist.Where(x => x.Id == d.Id).ToList().Count > 0)
{

uplist.Where(x => x.Id == d.Id).ToList()[0].Name = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Name"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].CodeName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["CodeName"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].EnName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnName"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].EnAbbreviation = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnAbbreviation"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].Nickname = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Nickname"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].Sort = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Sort"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].PhoneRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["PhoneRegular"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].IDNumberRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["IDNumberRegular"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].IsShow = MsParse.ToBoolean(dataGridView1.Rows[RowIndex].Cells["IsShow"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].OrderNum = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["OrderNum"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].OrderType = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["OrderType"].Value);

return;
}
d.Name = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Name"].Value);
d.CodeName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["CodeName"].Value);
d.EnName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnName"].Value);
d.EnAbbreviation = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnAbbreviation"].Value);
d.Nickname = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Nickname"].Value);
d.Sort = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Sort"].Value);
d.PhoneRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["PhoneRegular"].Value);
d.IDNumberRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["IDNumberRegular"].Value);
d.IsShow = MsParse.ToBoolean(dataGridView1.Rows[RowIndex].Cells["IsShow"].Value);
d.OrderNum = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["OrderNum"].Value);
d.OrderType = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["OrderType"].Value);
uplist.Add(d);
}


确定修改按钮

private void Edit_Click(object sender, EventArgs e)
{
int isqd = (int)MessageBox.Show("确定保存修改?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
if (isqd != 1)
{
return;
}
dataGridView1.CommitEdit((DataGridViewDataErrorContexts)123);
dataGridView1.BindingContext[dataGridView1.DataSource].EndCurrentEdit();
if (uplist.Count <= 0)
{
return;
}
if (db.UpDate(uplist))
{
uplist = new List<Models.CountryList>();
GetList();
}
else
{
MessageBox.Show("操作失败,请稍后再试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

本文共计626个文字,预计阅读时间需要3分钟。

如何通过Winform实现双击表格单元格直接修改数据的功能?

在表格中,当单元格值更改时,除了第一行外,其他行的更改将执行更新操作。以下是简化后的代码:

csharppublic List CountryList_uplist=new List();

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){ if (e.RowIndex !=-1) { // 更新操作 }}

表格触发事件

如何通过Winform实现双击表格单元格直接修改数据的功能?


public List<Models.CountryList> uplist = new List<Models.CountryList>();

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
return;
}
Upuplist(e.RowIndex);
}

public void Upuplist(int RowIndex)
{
Models.CountryList d = new Models.CountryList();
d.Id = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Id"].Value);
if (uplist.Where(x => x.Id == d.Id).ToList().Count > 0)
{

uplist.Where(x => x.Id == d.Id).ToList()[0].Name = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Name"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].CodeName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["CodeName"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].EnName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnName"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].EnAbbreviation = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnAbbreviation"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].Nickname = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Nickname"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].Sort = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Sort"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].PhoneRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["PhoneRegular"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].IDNumberRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["IDNumberRegular"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].IsShow = MsParse.ToBoolean(dataGridView1.Rows[RowIndex].Cells["IsShow"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].OrderNum = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["OrderNum"].Value);
uplist.Where(x => x.Id == d.Id).ToList()[0].OrderType = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["OrderType"].Value);

return;
}
d.Name = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Name"].Value);
d.CodeName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["CodeName"].Value);
d.EnName = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnName"].Value);
d.EnAbbreviation = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["EnAbbreviation"].Value);
d.Nickname = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["Nickname"].Value);
d.Sort = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["Sort"].Value);
d.PhoneRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["PhoneRegular"].Value);
d.IDNumberRegular = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["IDNumberRegular"].Value);
d.IsShow = MsParse.ToBoolean(dataGridView1.Rows[RowIndex].Cells["IsShow"].Value);
d.OrderNum = MsParse.ToString(dataGridView1.Rows[RowIndex].Cells["OrderNum"].Value);
d.OrderType = MsParse.ToInt32(dataGridView1.Rows[RowIndex].Cells["OrderType"].Value);
uplist.Add(d);
}


确定修改按钮

private void Edit_Click(object sender, EventArgs e)
{
int isqd = (int)MessageBox.Show("确定保存修改?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
if (isqd != 1)
{
return;
}
dataGridView1.CommitEdit((DataGridViewDataErrorContexts)123);
dataGridView1.BindingContext[dataGridView1.DataSource].EndCurrentEdit();
if (uplist.Count <= 0)
{
return;
}
if (db.UpDate(uplist))
{
uplist = new List<Models.CountryList>();
GetList();
}
else
{
MessageBox.Show("操作失败,请稍后再试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}