首页 > 解决方案 > 如何在不使用 SQL 查询的情况下更新 dataGridView 中的数据

问题描述

我正在尝试在 dataGridView 中进行 CRUD 操作,而不使用 SQL 查询,只使用 dataSet。deletecreate功能还可以,但我坚持如何实现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.");
        }

标签: c#winformsdatagridview

解决方案


除了使用数据集,您还可以使用人员对象的 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; }
    }


推荐阅读