GridView Column Formatting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poe
    New Member
    • Jun 2007
    • 32

    GridView Column Formatting

    Hello,

    I have an ASP.NET page that contains a gridview:
    Code:
    <asp:GridView ID="grdLogData" runat="server" BorderStyle="None" CellPadding="5" AllowPaging="True" GridLines="None">
      <RowStyle HorizontalAlign="Left" />
      <HeaderStyle Font-Bold="False" Font-Underline="True" HorizontalAlign="Left" CssClass="page-column-label" />
      <EditRowStyle CssClass="lightrow" VerticalAlign="Top" />
      <AlternatingRowStyle CssClass="darkrow" VerticalAlign="Top" />
    </asp:GridView>
    This gridview gets it data on the server-side from the .NET code:
    Code:
    DBDataAdapter = New SqlDataAdapter(DBQuery, DBConnection)
    DBDataTable = New DataTable("VTSLog")
    DBDataAdapter.Fill(DBDataTable)
    
    grdLogData.DataSource = DBDataTable
    grdLogData.DataBind()
    I would like to right-align the list items on columns 4, 5 and 8. Any suggestions?

    By the way, I'm targeting the 2.0 .NET framework using Visual Studio 2005.

    Thanks,
    Justin
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There is probably a way you can apply this to a column as a whole, but try this:
    Code:
    for (int i = 0; i < grdLogData.Rows.Count; i++)
    {
       grdLogData.Rows[i].Cells[4].HorizontalAlign =HorizontalAlign.Right;
       grdLogData.Rows[i].Cells[5].HorizontalAlign =HorizontalAlign.Right;
       grdLogData.Rows[i].Cells[8].HorizontalAlign =HorizontalAlign.Right;
    }

    Edit: It's possible this will work:
    Code:
    grdLogData.Columns[4].HorizontalAlign =HorizontalAlign.Right;
    grdLogData.Columns[5].HorizontalAlign =HorizontalAlign.Right;
    grdLogData.Columns[8].HorizontalAlign =HorizontalAlign.Right;

    Comment

    • poe
      New Member
      • Jun 2007
      • 32

      #3
      Thanks for the advice. The first method you suggested works properly. The second method does not work. Both before and after the data bind the grdLogData.Colu mns.Count is zero; apparently the columns aren't registered since they aren't specified manually.

      Thanks Again,
      Justin

      Comment

      Working...