2

I have a Griview which i bind dynamically. The fisrt column is the ID and i have a button to do things when the user press it depend on the ID of the row. I want to hide that column. I try every solution i found here how to change the column width but i can hide it or set it to 0. I try the

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If (e.Row.RowType = DataControlRowType.DataRow) Then
        e.Row.Cells(1).Width = New Unit("1px")
    End If

The code is working because if i type 1000px then its wider but i can't make it 0. The code for the grid is

    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns ="false"  Font-Size="Small" ShowFooter="True" Width="100%" GridLines="Both" BorderStyle="Solid" HeaderStyle-HorizontalAlign ="Center" >
        <Columns>

            <asp:ButtonField ButtonType="Button" CommandName="AddToBasket" Text="" />
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" ShowHeader="False" SortExpression="ID" Visible="true" />

What i'm doing wrong?

Ruby Kousinovali
  • 337
  • 2
  • 20
  • 1
    Add the property `DataKeyNames="ID"` to your `asp:GridView` and use `GridView1.DataKeys[rowIndex].Value` to obtain it. There's no sense in having a column that you never plan on displaying. [Here's a good link](http://www.codeproject.com/Articles/23833/DataKeyNames) on how to achieve it. – TestWell Aug 28 '15 at 21:58
  • @TestWell: Please post this as answer. This should be the way its done. – naveen Aug 29 '15 at 09:26
  • if you wanna hide a column, the event is `OnRowCreated` and not `OnRowDataBound`. see this http://stackoverflow.com/a/4954976/17447 – naveen Aug 29 '15 at 09:29
  • OK, but how to take the ID inside the GridView1_RowDataBound? – Ruby Kousinovali Aug 29 '15 at 13:35

2 Answers2

1

You can achieve what you are looking for by adding CSS.
It's worth moving your column to the end of the table.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

      e.Row.Cells[GridView1.Columns.Count-1].Style.Add("display", "none");

        ...
    }
Jury Golubev
  • 334
  • 2
  • 5
0

I saw that this question never had an accepted answer, The answer above are correct but I think you are a bit of a novice like me, You you need to do is to use CssClass in bootstrap you would us at as follows.

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" ForeColor="Black" HorizontalAlign="Center" Width="100%" CellPadding="4" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" >
        <Columns>
            <asp:BoundField DataField="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID">
            <HeaderStyle Width="1px" CssClass="hidden-lg" />
            <ItemStyle ForeColor="White" Width="1px" CssClass="hidden-lg" />
            </asp:BoundField>

Without Bootstrap create a class in your style tag set width to 0 and apply it to your boundFeild iteamStyle tag. This cracked me for many months solved it today.