【问题标题】:Delete Row and Save DataGridView删除行并保存 DataGridView
【发布时间】:2015-05-06 04:34:48
【问题描述】:

我没有在 C# 表单应用程序中使用 DataGridViews 做太多事情,所以我确信我的方法是一个新手。任何帮助都会很棒!

我的表单正在显示我的数据库的 DataGridView,我想让用户选择删除选定的特定行,然后保存更改。

这是我的代码:

    private void btnDeleteCustomer_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
        {
            dataGridView1.Rows.RemoveAt(item.Index);
            dataGridView1.SelectAll();

        }
    }

    private void btnSelectAll_Click(object sender, EventArgs e)
    {
        dataGridView1.SelectAll();
    }

我正在尝试删除选定的行,然后选择所有剩余的行,然后单击一个按钮保存选定的剩余行。我确信有更好的方法,但我找不到有效的解决方案。谢谢!

【问题讨论】:

  • 为什么要在删除一行后全选。另外,保存剩余的行是什么意思,是将其保存到数据库或文件中,还是只是试图刷新gridview。
  • 删除后我选择全部,因为我计划将选定的行保存回数据库,但我不确定如何执行此操作。
  • 但是你不需要全选,你只需要遍历所有剩余的行并保存到数据库中,
  • 我如何将剩余的保存到数据库中?
  • 为什么他会在删除一行的同时循环并保存项目?

标签: c# winforms datagridview delete-row savechanges


【解决方案1】:

我看不出有什么大的区别,但是你可以用不同的方式删除行,你可能想考虑使用扩展方法来轻松扩展datagridview功能,例如:

public static class DataGridViewExtensions
    {
        public static void DeleteSelectedRows(this DataGridView dgv)
        {
            foreach (DataGridViewRow row in dgv.SelectedRows)
                dgv.Rows.Remove(row);
        }
    }

你可以像这样简单地调用这个函数

dataGridView1.DeleteSelectedRows();

【讨论】:

  • 谢谢哈立德!使用 dataGridView1.DeleteSelectedRows(); 后出现此错误错误 1“System.Windows.Forms.DataGridView”不包含“DeleteSelectedRows”的定义,并且找不到接受“System.Windows.Forms.DataGridView”类型的第一个参数的扩展方法“DeleteSelectedRows”(您是否缺少使用指令还是程序集引用?)
  • 请确保它们(方法定义/调用者)在同一个命名空间下,如果不考虑使用您定义的命名空间
  • 您的表单找不到您拥有的扩展方法,因为它在不同的命名空间下,您可能缺少“使用指令”
【解决方案2】:
        Int32 rowToDelete = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);
        if (rowToDelete > -1)
        {
          dataGridView1.Rows.RemoveAt(rowToDelete);

          dataGridView1.Invalidate();
          string lastName = (string)dataGridView1.Rows[rowToDelete].Cells[0].Value;
          string firstName = (string)dataGridView1.Rows[rowToDelete].Cells[1].Value;
          string email = (string)dataGridView1.Rows[rowToDelete].Cells[2].Value;
          string phoneNumber = (string)dataGridView1.Rows[rowToDelete].Cells[3].Value;
          string address = (string)dataGridView1.Rows[rowToDelete].Cells[4].Value;
          string instagram = (string)dataGridView1.Rows[rowToDelete].Cells[5].Value;
          string carMake = (string)dataGridView1.Rows[rowToDelete].Cells[6].Value;
          string carModel = (string)dataGridView1.Rows[rowToDelete].Cells[7].Value;
          string additionalNotes = (string)dataGridView1.Rows[rowToDelete].Cells[8].Value;
          SqlConnection CustomerInfo = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Cory\\Desktop\\DeluxWrapsWindows\\DeluxWrapsWindows\\DeluxWraps.mdf;Integrated Security=True;User Instance=True");
          {
             SqlCommand xp = new SqlCommand("DELETE FROM CustomerInfo WHERE LastName='" + lastName + "' AND FirstName='" + firstName + "'");

             CustomerInfo.Open();
             xp.ExecuteNonQuery();
             CustomerInfo.Close();
          }
        }

【讨论】:

  • 当我添加代码时,我在“单元格”下有一条波浪线。我不确定这是为什么。另外,第三段代码是保存部分吗?非常感谢您的帮助!
  • 你如何填充dataGridView,发布代码,我可以根据dataGrid的列数修改答案。每个单元格都属于 dataGridView 中的一列。
  • 我在您发布的代码下方添加了我拥有的代码。我假设你正在寻找这个。我使用 dataGridView 直接从 Visual Studios 的数据库中发布数据,所以我自己没有编写任何代码。
  • 您需要找到如何将此代码保存到数据库中,您发布的代码仅是 Visual Studio 生成的设计器代码,我只保留了 dataGridView 部分并根据设计者指示的列数
  • 再次感谢 Faljbour!我仍在尝试消除代码中“Cel”一词的错误。有什么线索可以解释这是什么原因吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多