c# - 如何在不使用 SQL 查询的情况下更新 dataGridView 中的数据
问题描述
我正在尝试在 dataGridView 中进行 CRUD 操作,而不使用 SQL 查询,只使用 dataSet。delete
和
create
功能还可以,但我坚持如何实现update
.
要求是当用户单击 dataGridView 中的数据单元格时,该单元格的所有数据将显示在已创建的列表中textBoxes
,并且可以编辑或更新这些数据。所有字段的数据都可以更改,但id
行的数据必须保持不变。
我的想法是dataGridView_CellContentClick
在单击单元格时使用 a 。所有数据都将显示在已创建的textboxes
. 我们需要id
通过使用类成员变量来获取我们想要编辑的行来存储它。但是在最后一步我不知道如何将更新的数据分配给dataGridView的行。有没有人有任何解决方案?
这是我的代码:
public partial class Form1 : Form
{
// data set
DataSet1 dSet;
// to get id of current row
int id;
public Form1()
{
InitializeComponent();
PersonInfoInit();
}
// udpate cell clicked data
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// get id of the row want to update
id = dtgView.CurrentCell.RowIndex;
UpdateData();
}
// update current cell clicked data helper function
private void UpdateData()
{
if (dtgView.CurrentRow.Index != -1)
{
txtAge.Text = dtgView.CurrentRow.Cells[1].Value.ToString();
txtName.Text = dtgView.CurrentRow.Cells[2].Value.ToString();
txtEmail.Text = dtgView.CurrentRow.Cells[3].Value.ToString();
}
}
private void PersonInfoInit()
{
dSet = new DataSet1();
dSet.Tables["Person"].Rows.Add(new object[] { 1, "Kane", 23, "abc@gmail.com" });
dSet.Tables["Person"].Rows.Add(new object[] { 2, "Sam", 22, "abc@gmail.com"});
dSet.Tables["Person"].Rows.Add(new object[] { 3, "Hung", 24, "haha@naver" });
dSet.Tables["Person"].Rows.Add(new object[] { 4, "Tuan", 26, "blusPai@hanmail"});
dtgView.AutoGenerateColumns = true; // without this line, datatGridView will not display data
dtgView.DataSource = dSet.Tables["Person"];
}
private void btnAdd_Click(object sender, EventArgs e)
{
dSet.Tables["Person"].Rows.Add(new object[]
{
dSet.Tables["Person"].Rows.Count + 1, int.Parse(txtAge.Text), txtName.Text.Trim(), txtEmail.Text.Trim()
});
ClearText();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
// how can I assign updated data for the row? not Add newly
// dSet.Tables["Person"].Rows[id]. = new object[] {
// };
}
// delete current clicked cell data
private void btnDel_Click(object sender, EventArgs e)
{
try
{
foreach (DataGridViewCell cell in dtgView.SelectedCells)
{
if (cell.Selected)
dtgView.Rows.RemoveAt(cell.RowIndex);
}
}
catch
{
MessageBox.Show("ERROR: can't remve this data");
}
MessageBox.Show("Removed data successfully.");
}
解决方案
除了使用数据集,您还可以使用人员对象的 BindingList 并添加、删除或修改该列表中的对象。
private BindingList<Person> Persons = new BindingList<Person>();
....
private void PersonInfoInit()
{
this.Persons.Add(new Person() { Id = 1, Name = "Kane", Age = 23, Email = "abc@gmail.com" });
this.Persons.Add(new Person() { Id = 2, Name = "Sam", Age = 2, Email = "abc@gmail.com" });
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = this.Persons;
}
...
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
推荐阅读
- typo3 - FAL FileUpload 中不允许使用 TYPO3 10.4.0 中的 ext:form 序列化“Closure”
- javascript - 从表中添加和删除多行
- python - Networkx:通过循环遍历节点列表添加属性时,IndexError:列表索引超出范围
- r - R并行包错误:一个节点产生错误:没有适用的“收敛”方法应用于“try-error”类的对象
- python - 如何匹配括号外的特定单词并忽略括号中的单词?
- ios - 将 MacOS 更新到 1.015.4 和 Xcode 更新到 11.4 后,Qt Creator 应用程序无法构建,如何解决?
- openmdao - 在组件上使用复杂步骤必须满足哪些要求?
- normalization - PCA 和 SVD 对相同单位数据的不同结果
- azure - Azure AD 自定义声明作为单独的属性值
- arduino - 在 arduino IDE 中使用修改后的库