Set the GridView AutoFormat programmatically

Last post 11-04-2007 9:07 PM by netplan. 4 replies.

Sort Posts:

  • Set the GridView AutoFormat programmatically

    11-02-2007, 1:58 PM
    • Member
      85 point Member
    • netplan
    • Member since 11-16-2003, 10:07 AM
    • Posts 156

    Hi,

    I'm displaying a GridView programmatically in my code and I was wondering if there was a way to set the AutoFormat programmatically to use "Professional"?

    Thanks

  • Re: Set the GridView AutoFormat programmatically

    11-02-2007, 4:30 PM
    Answer
    • Participant
      752 point Participant
    • ubaid1900
    • Member since 05-25-2005, 8:26 PM
    • Posts 141

    when you apply the "Professional" format in the designer, the plain grid view converts as below. Copy this and paste it into a skin file. Remove the ID="GridView1" This formatting gets applied to all GridViews.

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <EditRowStyle BackColor="#999999" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>

     If you do not want it across all GridViews, the add SkinId="ProfessionalSkin" attribute, the set the GridView's SkinId property to "ProfessionalSkin"
     

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" SkidId="ProfessionalSkin">
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <EditRowStyle BackColor="#999999" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>

  • Re: Set the GridView AutoFormat programmatically

    11-04-2007, 12:44 PM
    • Member
      85 point Member
    • netplan
    • Member since 11-16-2003, 10:07 AM
    • Posts 156

    Ahhh so that's the secret.

    Thanks ubaid1900.

  • Re: Set the GridView AutoFormat programmatically

    11-04-2007, 12:52 PM
    • Member
      95 point Member
    • InsanePaul
    • Member since 10-31-2007, 9:31 AM
    • Posts 358

    hi, i'm stuck trying to create a gridview programmatically. Can you help please?

  • Re: Set the GridView AutoFormat programmatically

    11-04-2007, 9:07 PM
    • Member
      85 point Member
    • netplan
    • Member since 11-16-2003, 10:07 AM
    • Posts 156

    Hi InsanePaul,

    Here' what I've got. I'm still working on it to make it more dynamic but you'll get the idea on how to create it through code. I've created a Game object that retrieves the data I want. 

    I'm using BoundFields here instead of AutoGenerateColumns. If you use AutoGenerateColumns then you don't need to create all these BoundFields.

    I've also added a Panel on my ASP page called pnlGames and you'll see at the end of this routine where I add my GridView to the panel.

    Hope this helps.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim mygame As New Game   
    
        Dim grv1 As New GridView
    
        grv1.AutoGenerateColumns = False
        grv1.CssClass = "contentTable"
        grv1.GridLines = GridLines.Both
    
    
        Dim bfldGameDate As New BoundField
        With bfldGameDate
          .HeaderText = "Game Date"
          .DataField = "game_date"
          .DataFormatString = "{0:MM/dd/yy}"
          .HtmlEncode = False
        End With
        grv1.Columns.Add(bfldGameDate)
    
        Dim bfldGameTime As New BoundField
        With bfldGameTime
          .HeaderText = "Game Time"
          .DataField = "game_time"
          .DataFormatString = "{0:hh:mm tt}"
          .HtmlEncode = False
        End With
        grv1.Columns.Add(bfldGameTime)
    
        Dim bfldHome As New BoundField
        With bfldHome
          .HeaderText = "Home"
          .DataField = "home_team"
        End With
        grv1.Columns.Add(bfldHome)
    
        Dim bfldAway As New BoundField
        With bfldAway
          .HeaderText = "Away"
          .DataField = "away_team"
        End With
        grv1.Columns.Add(bfldAway)
    
        With grv1
          .DataSource = mygame.GetGamesByDate("21/09/2007 12:00:00 AM")
          .DataBind()
        End With
    
        pnlGames.Controls.Add(grv1)
    
    
        End Sub
     
Page 1 of 1 (5 items)