havin havin - 11 days ago 7
Vb.net Question

Remove Edit Option in Gridview

My GridView is as follows

It has EmptyDatatemplate and Command Field

<asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true" style="table-layout:fixed;" Width="2000px" RowStyle-HorizontalAlign="Left">
<EmptyDataTemplate>
&nbsp;
</EmptyDataTemplate>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit">
<ItemStyle Font-Bold="true" Font-Size="Small" />
<HeaderStyle CssClass="AAddOn" />
</asp:CommandField>
</asp:GridView>


GridView Looks like(it has only 2 rows)

Name Age Country
A 10 NNN Edit
B 23 NNN Edit


Now i dont need Edit Button to be displayed in first row.How can i do it.

Only 2 rows will be displayed here.

Name Age Country
A 10 NNN
B 23 NNN Edit


Here header is count of Gridview Header cells and my edit is in last cell.
(My Gridview generated dynamically generated columns but has only 2 rows so i cannot take fixed column values hence used header count)

Dim Header As Integer
For counts = 0 To AGridView.HeaderRow.Cells.Count
Header = counts
Next
Dim edit as LinkButton = DirectCast(AGridView.Rows(0).cell(header).FindControl("Edit"),LinkButton)
edit.Visible = False


Error message for above is Object reference not set to instance.Index is out of range.

Next I have tried inside AGridView_Rowdatabound as but whole cell is disappearing .I need only First row edit linkbutton visible to be false

If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(Header).Visible = False
End If


What am i missing here.I need only First row edit button visible to be false.

Answer

Based on the discussion in the comment section of the question, assuming that you want to disable Edit button only for the first data row and that the button is in the 4th column, I suggest trying this:

Add the OnRowDataBound declaration to the markup:

<asp:GridView ID="AGridView" ... OnRowDataBound="AGridView_RowDataBound">
    ...
</asp:GridView>

Add RowDataBound event handler to the code behind file:

Protected Sub AGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowIndex = 1 Then
        e.Row.Cells(3).Visible = False
    End If
End Sub
Comments