0

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.

havin
  • 203
  • 4
  • 17
  • 1
    Please take a look at http://stackoverflow.com/questions/1461302/conditionally-hide-commandfield-or-buttonfield-in-gridview. – davke Nov 24 '16 at 06:42
  • Hi i looked into the solution but i dont have boundfields..Since my gridview shows only 2 rows ,how can i do this in gridview rowdatabound.I mean like row(1) button visible should be false likewise.. – havin Nov 24 '16 at 07:25
  • 1
    I don't think it matters. The idea in the accepted answer there is to replace CommandField (which you do have) with ItemTemplate where you can create Edit command button for which you can conditionally set the Visible property. Alternativelly you can try the proposed solution at the bottom of the answer here http://stackoverflow.com/a/7188143/4955259. It seems to use the CommandField and the RowDataBound event handler instead. – davke Nov 24 '16 at 07:38
  • Hi i have tried your link but i cannot get solution..I have edited my question of what have i tried..I guess iam missing something – havin Nov 24 '16 at 09:33

1 Answers1

2

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
davke
  • 350
  • 1
  • 7
  • Thanks Working fine. – havin Nov 24 '16 at 10:52
  • Hi i have one follow up question..While making visible =false..My Gridview border for particular cell vanishes.Here for Name A my edit is invisible also border for particular cell also vanishes .. – havin Nov 24 '16 at 10:54
  • 1
    I think that is because the html for the cell is not rendered at all, once the GridViewCell's Visible property is set to false. Can you try changing the line `e.Row.Cells(3).Visible = False` to `e.Row.Cells(3).Controls.Cast(Of Control)().First().Visible = False` and then say if it helped? – davke Nov 25 '16 at 07:21
  • Thanks for the solution..I have done like e.Row.Cells(3).Text =string.empty which works perfectly.. – havin Nov 25 '16 at 11:55