【问题标题】:How can I delete entire row in my SQL datagridview?如何删除我的 SQL datagridview 中的整行?
【发布时间】:2014-12-29 17:51:13
【问题描述】:

如何删除我的 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>

当我点击删除链接时,会显示此错误

“GridView 'EmployeeHallway' 触发了未处理的事件 RowDeleting。”

谁能帮我下一步该怎么做

【问题讨论】:

标签: asp.net sql-server database vb.net vba


【解决方案1】:

您使用Delete 作为删除链接的CommandName,因此它会自动创建RowDeleting 事件。所以你必须像这样实现它:

你必须添加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"/>

在代码隐藏处:

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

End Sub

【讨论】:

  • 如果我错了,请纠正我,但这段代码适用于 c# 对吗?先生,我需要 VB 环境的代码。
【解决方案2】:

在aspx页面中添加OnRowDeleting="OnRowDeleting"

将您的数据表保存在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

【讨论】:

  • BindGrid() 出现错误“未声明 BindGrid。”
  • 发生此错误“对象引用未设置为对象的实例。”
  • 您是否将您的DataTable 保存在ViewState("dt") 中??
  • 如何保存我的数据表?数据表和数据gridview是一样的吗?呵呵,我还是这个对不起的新手
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多