删除datagridview和数据库中的多行数据 [英] deleting the multiple rowsof data in datagridview as well as in database

查看:75
本文介绍了删除datagridview和数据库中的多行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何使用C#windows窗体按钮从数据网格视图中删除数据库和选定行?

I wish to ask how i can delete the both database and selected row from data grid view by using C# windows form button??

推荐答案

您的.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");

      }


这篇关于删除datagridview和数据库中的多行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆