winform 双击表格里的数据进行修改

时间:2022-10-28 19:58:48

表格触发事件


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);
}
}