![](/img/trans.png)
[英]When exporting gridview to xls, the entire row is formatted. How can I limit this to my gridview columns
[英]How can I delete entire row in my SQL datagridview?
如何在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。”
誰能幫我下一步做什么
您正在使用Delete
作為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
在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.