【问题标题】:How to update data in dataGridView not using SQL query如何在不使用 SQL 查询的情况下更新 dataGridView 中的数据
【发布时间】:2021-06-17 15:19:19
【问题描述】:

我正在尝试在 dataGridView 中进行 CRUD 操作,不使用 SQL 查询,只使用 dataSet。 deletecreate 功能还可以,但我一直坚持如何实现 update

要求是当用户单击 dataGridView 中的数据单元格时,然后该单元格的所有数据 将显示在已创建的textBoxes 列表中,并且可以编辑或更新这些数据。所有字段的数据都可以更改,但行的id必须保持不变。

我的想法是在单击单元格时使用dataGridView_CellContentClick。所有数据都将显示在已创建的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.");
        }

【问题讨论】:

  • 让用户“直接”在网格单元格中进行更改不是更容易吗?是否需要一些特殊的数据验证?
  • 只需使用 BindingSource,将其用作 DGW 的 DataSource,并使用相同的 BindingSource 将 DataBindings 添加到 TextBoxes。当您单击单元格时,您还会更改 Current 引用:您的 TextBox 控件将反映当前 DataRow,并且 TextBox 文本中的任何更改都会自动更新数据源。
  • @JohnG 你能给我更多关于网格单元中“直接更改”的详细信息吗?不,不需要数据验证。
  • @Jimi 我不确定我能理解你的想法,你能在 dataGridView 上使用 BindingSource 提供一些 sn-ps 吗?
  • 网格加载数据后,您应该能够简单地单击单元格并输入您想要的内容。显然,如果单元格是数字单元格,您可能希望只允许数字以避免错误。但是,如果单元格是“文本”单元格,从技术上讲,您应该能够在单元格中输入任何内容。这些更改将自动反映在网格数据源中。我好像错过了什么。

标签: c# winforms datagridview


【解决方案1】:

您可以使用人员对象的 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; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多