简体   繁体   English

如何删除SQL datagridview中的整个行?

[英]How can I delete entire row in my SQL datagridview?

How can I delete an entire row in my datagridview.. I already have a delete link in my datagrid.. Here is my markup code in vb 如何在datagridview中删除整行。.我在datagrid中已经有一个删除链接。.这是我在vb中的标记代码

 <asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
              <AlternatingRowStyle BackColor="White" />
              <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>

                               <%--ADD THE DELETE LINK BUTTON--%>
                               <asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick ="return confirm('Are you sure you?');"
                                   CommandName="Delete">Delete</asp:LinkButton>

                        </ItemTemplate>
                     </asp:TemplateField>
                  <asp:BoundField DataField="EmployeeID" HeaderText="Locker ID" />
                  <asp:BoundField DataField="Name" HeaderText="Name" />
                  <asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo" />
                  <asp:BoundField DataField="Email" HeaderText="Email" />
                  <asp:BoundField DataField="Location" HeaderText="Location" />
              </Columns>
              <HeaderStyle BackColor="#003366" BorderColor="#336699" BorderStyle="Solid" ForeColor="White" />
              <PagerStyle BackColor="#003366" BorderColor="#336699" ForeColor="White" />
              <RowStyle BackColor="White" ForeColor="#003366" />
              <SelectedRowStyle BackColor="White" ForeColor="#6600FF" />
              <SortedAscendingCellStyle BackColor="#CCCCCC" />
 </asp:GridView>

And when I click the delete link this error shows 当我单击删除链接时,将显示此错误

"The GridView 'EmployeeHallway' fired event RowDeleting which wasn't handled. " “ GridView'EmployeeHallway'引发了未处理的事件RowDeleting。”

Can anyone help me what to do next 谁能帮我下一步做什么

You are using Delete as the CommandName for the delete link so it will automatically creates a RowDeleting event. 您正在使用Delete作为Delete链接的CommandName ,因此它将自动创建RowDeleting事件。 So you have to implement it like this: 因此,您必须像这样实现它:

You have to add OnRowDeleting event as below: 您必须添加OnRowDeleting事件,如下所示:

<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
    <RowStyle ForeColor="#003399" HorizontalAlign="Center" OnRowDeleting="EmployeeHallway_RowDeleting"/>

And at the code-behind: 而在后面的代码中:

Public Sub EmployeeHallway_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)

End Sub

Add OnRowDeleting="OnRowDeleting" in aspx page 在aspx页面中添加OnRowDeleting="OnRowDeleting"

Saving Your DataTable in ViewState("dt") and then Do Like this: 将数据表保存在ViewState("dt") ,然后执行以下操作:

Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
    Dim index As Integer = Convert.ToInt32(e.RowIndex)
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
    dt.Rows(index).Delete()
    ViewState("dt") = dt
    BindGrid()
End Sub

 Protected Sub BindGrid()
    EmployeeHallway.DataSource = TryCast(ViewState("dt"), DataTable)
    EmployeeHallway.DataBind()
End Sub

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

相关问题 将gridview导出到xls时,将格式化整个行。 如何将其限制为我的gridview列 - When exporting gridview to xls, the entire row is formatted. How can I limit this to my gridview columns 如何在 SSRS 报告中将整行设置为超链接? - How can i set entire row as hyperlink in SSRS report? 如何将行删除按钮连接到GridView_RowDeleted事件? - How can I hook up a row delete button to my GridView_RowDeleted event? 如何在SQL Server中修剪整个select语句? - How can I trim the entire select statement in SQL Server? 从DataGridView的SQL删除没有发生 - SQL Delete from DataGridView is not happening 如何在Telerik的RadListBox控件上添加和删除行? - How can I add and delete row on Telerik's RadListBox Control? 如何在没有数据库查询的情况下删除gridview中的行 - how I can delete row in gridview without queries in DB C#/DataTable/DataGrid/SQL - 如何从 DataGridView 将参数插入到存储过程中 - C#/DataTable/DataGrid/SQL - How can I insert Params into a stored procedure from DataGridView 如何从我的sql数据库中删除多余的记录,并且只有1行, - How to delete extra record from my sql database and Have only 1 row, 从datagridview和datasource表中删除选定的行 - delete selected row from datagridview and datasource table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM