Configuring Columns of a GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marjani
    New Member
    • May 2010
    • 2

    Configuring Columns of a GridView

    Hi,

    I want to Chang header file of Gridview so i use this code :

    GridView1.Colum ns[1].HeaderText = "nummber";

    But it doesn't work !! :(

    and

    I don't know how can i show just 3 Columns of 7 Columns of table in GridView???

    Please Help Me .

    thank.
  • AnagJohari
    New Member
    • May 2010
    • 96

    #2
    Originally posted by marjani
    Hi,

    I want to Chang header file of Gridview so i use this code :

    GridView1.Colum ns[1].HeaderText = "nummber";

    But it doesn't work !! :(

    and

    I don't know how can i show just 3 Columns of 7 Columns of table in GridView???

    Please Help Me .

    thank.
    if u want to show three column in a grid view u just bound those field u want to display in a grid view
    so simpleeeeeeee

    By the way why u change the header name of an column by code behind , u can do easily by the aspx page.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Marjani,

      If you want to have more control over your GridView and what it displays you should consider configuring it so that it does not auto-generate the columns for you.

      You can do this by setting the AutoGenerateCol umns property to False.

      Once you've done this you have to manually go in set the columns you want to display.

      You can do this using the Visual Studio IDE: in Design view of your page, click on the GridView, look at the Properties for the GridView, find the Columns property and click the "..." button to add Columns.

      You can do this in Source View of your page (this is the method that I prefer)...find the <asp:GridView > element...make sure it's AutoGenerateCol umns property is set to False, and add your columns into the <columns> section within the GridView.

      For Example:
      Code:
      <asp:GridView ID="myGrid" runat="server" AutoGenerateCollumns="False">
        <Columns>
            <asp:BoundField DataField="abc" HeaderText="Description for ABC" />
            <asp:BoundField DataField="lmnop" HeaderText="Description for LMNOP" 
            <asp:BoundField DataField="xyz" HeaderText="Description for XYZ" />/>
        </Columns>
      </asp:GridView>
      Just make sure that the DataField property matches the name of the Data Field that the column is begin bound to or this wont work :)

      You can also do this dynamicallyin in your C# code but I'm not going to get into that because this method is complicated.

      -Frinny

      Comment

      Working...