且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除datagridview和数据库中的多行数据

更新时间:2023-12-04 12:43:40

您的.aspx页面网格代码。

Your .aspx page grid code.
<asp:templatefield headertext="Action" xmlns:asp="#unknown">
   <itemtemplate>                   
      <asp:linkbutton id="lblDelete" runat="server" commandname="DeleteRecord" commandargument="<%#Eval("Id") %>">Delete</asp:linkbutton>
   </itemtemplate>
</asp:templatefield>



并注意......




And Note that...

Here don't use commandname="Delete/delete/DELETE".
This are inbuilt keywords. IF you use this keywords it will call OnDelete event not OnRowCommand.
This will occur runtime exception that OnDelete event was not handled.  



您的代码删除记录。


Your Code To Delete the record.

protected void GVCategories_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument.ToString());

        //If Commandname == Delete Record then We will Proceed to delete the record
        if (e.CommandName == "DeleteRecord")
        {

            int result = 0;
            SqlCommand cmd = new SqlCommand();

           try
           {
               if (conn.State == ConnectionState.Closed)
               {
                   conn.Open();
               }

               cmd.CommandText = "DeleteCategory";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Connection = conn;
               cmd.Parameters.Add(new SqlParameter("@ID", id));
               cmd.CommandTimeout = 10;
               result = cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
               Console.WriteLine("Error occured in DAL.DeleteSelectedRow() \nMessage : " + ex.Message + "InnerException : " + ex.InnerException);
            }
            finally
            {
               cmd.Dispose();
               conn.Close();
            }
            
        }
        
    }





这里你的半工作已完成。

这将从数据库中删除数据。

要刷新网格,您需要的是从数据库获取新数据并使用新数据绑定网格。 br $> b $ b



希望这个帮助你

--------------- -----

Pratik Bhuva



Here your Half work is done.
This will delete data from database.
And to refresh the Grid what you need is get fresh data from database and bind the grid with that fresh data.


Hope This Help You
--------------------
Pratik Bhuva


检查:

插入更新-删除与 - datagridview的 - 控制 - 在-C#-Windows应用 [ ^ ]

使用分页在DataGridView中添加,编辑和删除 [ ^ ]
Check This:
Insert-update-delete-with-datagridview-control-in-c#-windows-application[^]
Add, Edit, and Delete in DataGridView with Paging[^]


private void btnDelete_Click(object sender, EventArgs e)
      {
          int i = 0;
          string str;
          List<int> ChkedRow = new List<int>();
          for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
          {
              if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Col1"].Value) == true)
              {
                  ChkedRow.Add(i);
              }
          }
          if (ChkedRow.Count == 0)
          {
              MessageBox.Show("Select one checkbox");
              return;
          }

          foreach (int j in ChkedRow)// deleting from Database
          {
              str = "Delete from eventdgv where Name='" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "'";
              try
              {
                  using (SqlCeConnection cn = new SqlCeConnection(connection))
                  {
                      using (SqlCeCommand cmd = new SqlCeCommand(str, cn))
                      {
                          cn.Open();
                          cmd.ExecuteNonQuery();
                      }
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }

          }
          for (int s = dataGridView1.Rows.Count - 1; s >= 0; s--)// Deleting from gridview
          {
              if (Convert.ToBoolean(dataGridView1.Rows[s].Cells["Col1"].Value) == true)
              {
                  dataGridView1.Rows.RemoveAt(s);
              }
          }

          disp();
          MessageBox.Show("deleted succesfully");

      }