简体   繁体   English

删除行并保存DataGridView

[英]Delete Row and Save DataGridView

I haven't done too much with DataGridViews in C# form applications so my approach is a rookie one I'm sure. 我没有在C#表单应用程序中使用DataGridViews做太多,所以我的方法是一个新手,我敢肯定。 Any help would be awesome! 任何帮助都是极好的!

My form is displaying the DataGridView for my database, and I want to give the user the option of deleting a particular row that is selected and then saving the changes. 我的表单显示我的数据库的DataGridView,我想让用户选择删除所选的特定行,然后保存更改。

Here is my code: 这是我的代码:

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

I am attempting to delete the selected row, then select all of the remaining rows, and then save the selected remaining rows with the click of one button. 我试图删除选定的行,然后选择所有剩余的行,然后单击一个按钮保存选定的剩余行。 I'm sure there is a better way of doing so but I can't find a solution that works. 我确信有更好的方法,但我找不到有效的解决方案。 Thanks! 谢谢!

I don't see that there's a big difference, But you could delete rows in different ways, You might want to consider using extension methods to extend the datagridview functionality with ease, For example: 我没有看到有很大的区别,但你可以用不同的方式删除行,你可能想要考虑使用扩展方法来轻松扩展datagridview功能,例如:

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

And you could simply call this function like this 你可以像这样简单地调用这个函数

dataGridView1.DeleteSelectedRows();
        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();
          }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM