Aligning Particular Columns in a Data Grid

Discussion in 'ASP .Net Web Controls' started by Jennifer, Nov 18, 2004.

  1. Jennifer

    Jennifer Guest

    Hi! I've got a datagrid on an asp page and I'd like to set a few of
    the columns to center-align. The rest should be left align.

    The datagrid (called dgDSR) has 61 columns. It is databound using a
    data table.

    The code I was trying out to center just the columns I want is:

    dgDSR.Columns(2).ItemStyle.HorizontalAlign = HorizontalAlign.Center

    I get the error message that the index is out of bounds. I thought
    that was odd, since there really are 61 columns. So I got the value
    of dgDSR.Items.Count and it was 1.

    So I guess my question is, can I change the alignment of certain
    columns? And if so, what is the proper way to do that?

    Thanks,
    Jennifer
     
    Jennifer, Nov 18, 2004
    #1
    1. Advertising

  2. Jennifer, are you using AutoGenerateColumns=False? If so, you can
    specify the alignment specifically, like so:

    <asp:BoundColumn ... ItemStyle-HorizontalAlign="Center" ... />

    If you are having the column automatically generated, you can
    programmatically set their alignment, it's just a matter of doing it at
    the right time. Namely, you have to do it after the call to the
    DataGrid's DataBind() method. So your code should look something like:

    DataGridID.DataSource = ...
    DataGridID.DataBind()

    'Code to set alignment of columns

    hth

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 18, 2004
    #2
    1. Advertising

  3. Jennifer

    Jennifer Guest

    Scott,

    Thank you for replying. The columns are being automatically generated. I
    do try to set the alignment after the data binding. I'm going to post my
    almost-full code below (I'm just not going to show the code for each of the
    61 columns - that would be too much.) Do you have any other suggestions?
    Perhaps the way I am setting up the grid in the first place is wrong?

    I appreciate any advice you can give me.

    Thanks,
    Jennifer


    Private Sub FillGrid(ByVal SortExp As String)
    Dim dvMain As DataView
    Dim X As Int64
    Dim dtMain As New DataTable()
    Dim DR As DataRow
    Dim Update_User As String
    If txtStartDate.Text <> "*" Then
    If txtEndDate.Text = "*" Then Exit Sub
    End If
    dvMain = GetDailySales(txtUnitID.Text, txtStartDate.Text,
    txtEndDate.Text)
    If SortExp <> "" Then
    dvMain.Sort = SortExp
    End If
    Dim Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10 As
    String
    Dim Col11, Col12, Col13, Col14, Col15, Col16, Col17, Col18, Col19,
    Col20 As String
    Dim Col21, Col22, Col23, Col24, Col25, Col26, Col27, Col28, Col29,
    Col30 As String
    Dim Col31, Col32, Col33, Col34, Col35, Col36, Col37, Col38, Col39,
    Col40 As String
    Dim Col41, Col42, Col43, Col44, Col45, Col46, Col47, Col48, Col49,
    Col50, Col51, Col52 As String
    Dim Col53, Col54, Col55, Col56, Col57, Col58, Col59, Col60, Col61,
    Col62, Col63, Col64 As String


    Col1 = "Unit"
    Col2 = "Date"
    Col3 = "Status"
    Col4 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$"
    Col55 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$" & vbCrLf &
    "Verified"
    Col5 = "Deposit" & vbCrLf & "Early_Bird" & vbCrLf & "$"
    '-----more column names set here------------

    dtMain.Columns.Add(Col1)
    dtMain.Columns.Add(Col2)
    dtMain.Columns.Add(Col3)
    dtMain.Columns.Add(Col4)
    If chkViewVerif.Checked = True Then dtMain.Columns.Add(Col55)
    '-----adding the rest of the columns to the data row here

    Dim BD As Date
    For X = 0 To dvMain.Count - 1
    DR = dtMain.NewRow
    DR(Col1) = dvMain(X)("Unit_ID")
    BD = dvMain(X)("Business_Date")
    DR(Col2) = BD.ToShortDateString
    DR(Col3) = dvMain(X)("Status")
    If IsDBNull(dvMain(X)("User_ID")) Then
    DR(Col50) = dvMain(X)("User_ID")
    Else
    Update_User = dvMain(X)("User_ID")
    DR(Col50) = Update_User.Substring(12)
    End If
    DR(Col4) = Format(dvMain(X)("Deposit_Eve_Amt"), "N")
    If chkViewVerif.Checked = True Then DR(Col55) =
    dvMain(X)("Deposit_Eve_Amt_Verified")
    '--- assigning the rest of the values here
    dtMain.Rows.Add(DR)
    Next

    dgDSR.DataSource = dtMain
    dgDSR.DataBind()
    '---trying to align a column here (and it doesn't work)
    dgDSR.Columns(2).ItemStyle.HorizontalAlign = HorizontalAlign.Center

    End Sub


    "Scott Mitchell [MVP]" wrote:

    > Jennifer, are you using AutoGenerateColumns=False? If so, you can
    > specify the alignment specifically, like so:
    >
    > <asp:BoundColumn ... ItemStyle-HorizontalAlign="Center" ... />
    >
    > If you are having the column automatically generated, you can
    > programmatically set their alignment, it's just a matter of doing it at
    > the right time. Namely, you have to do it after the call to the
    > DataGrid's DataBind() method. So your code should look something like:
    >
    > DataGridID.DataSource = ...
    > DataGridID.DataBind()
    >
    > 'Code to set alignment of columns
    >
    > hth
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
    >
     
    Jennifer, Nov 18, 2004
    #3
  4. And if you use the debugger to step through, you see that
    dtMain.Columns.Count > 1, but the DataGrid's Columns.Count property = 1?
    That is indeed odd.


    Jennifer wrote:
    > Scott,
    >
    > Thank you for replying. The columns are being automatically generated. I
    > do try to set the alignment after the data binding. I'm going to post my
    > almost-full code below (I'm just not going to show the code for each of the
    > 61 columns - that would be too much.) Do you have any other suggestions?
    > Perhaps the way I am setting up the grid in the first place is wrong?
    >
    > I appreciate any advice you can give me.
    >
    > Thanks,
    > Jennifer
    >
    >
    > Private Sub FillGrid(ByVal SortExp As String)
    > Dim dvMain As DataView
    > Dim X As Int64
    > Dim dtMain As New DataTable()
    > Dim DR As DataRow
    > Dim Update_User As String
    > If txtStartDate.Text <> "*" Then
    > If txtEndDate.Text = "*" Then Exit Sub
    > End If
    > dvMain = GetDailySales(txtUnitID.Text, txtStartDate.Text,
    > txtEndDate.Text)
    > If SortExp <> "" Then
    > dvMain.Sort = SortExp
    > End If
    > Dim Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10 As
    > String
    > Dim Col11, Col12, Col13, Col14, Col15, Col16, Col17, Col18, Col19,
    > Col20 As String
    > Dim Col21, Col22, Col23, Col24, Col25, Col26, Col27, Col28, Col29,
    > Col30 As String
    > Dim Col31, Col32, Col33, Col34, Col35, Col36, Col37, Col38, Col39,
    > Col40 As String
    > Dim Col41, Col42, Col43, Col44, Col45, Col46, Col47, Col48, Col49,
    > Col50, Col51, Col52 As String
    > Dim Col53, Col54, Col55, Col56, Col57, Col58, Col59, Col60, Col61,
    > Col62, Col63, Col64 As String
    >
    >
    > Col1 = "Unit"
    > Col2 = "Date"
    > Col3 = "Status"
    > Col4 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$"
    > Col55 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$" & vbCrLf &
    > "Verified"
    > Col5 = "Deposit" & vbCrLf & "Early_Bird" & vbCrLf & "$"
    > '-----more column names set here------------
    >
    > dtMain.Columns.Add(Col1)
    > dtMain.Columns.Add(Col2)
    > dtMain.Columns.Add(Col3)
    > dtMain.Columns.Add(Col4)
    > If chkViewVerif.Checked = True Then dtMain.Columns.Add(Col55)
    > '-----adding the rest of the columns to the data row here
    >
    > Dim BD As Date
    > For X = 0 To dvMain.Count - 1
    > DR = dtMain.NewRow
    > DR(Col1) = dvMain(X)("Unit_ID")
    > BD = dvMain(X)("Business_Date")
    > DR(Col2) = BD.ToShortDateString
    > DR(Col3) = dvMain(X)("Status")
    > If IsDBNull(dvMain(X)("User_ID")) Then
    > DR(Col50) = dvMain(X)("User_ID")
    > Else
    > Update_User = dvMain(X)("User_ID")
    > DR(Col50) = Update_User.Substring(12)
    > End If
    > DR(Col4) = Format(dvMain(X)("Deposit_Eve_Amt"), "N")
    > If chkViewVerif.Checked = True Then DR(Col55) =
    > dvMain(X)("Deposit_Eve_Amt_Verified")
    > '--- assigning the rest of the values here
    > dtMain.Rows.Add(DR)
    > Next
    >
    > dgDSR.DataSource = dtMain
    > dgDSR.DataBind()
    > '---trying to align a column here (and it doesn't work)
    > dgDSR.Columns(2).ItemStyle.HorizontalAlign = HorizontalAlign.Center
    >
    > End Sub
    >
    >
    > "Scott Mitchell [MVP]" wrote:
    >
    >
    >>Jennifer, are you using AutoGenerateColumns=False? If so, you can
    >>specify the alignment specifically, like so:
    >>
    >><asp:BoundColumn ... ItemStyle-HorizontalAlign="Center" ... />
    >>
    >>If you are having the column automatically generated, you can
    >>programmatically set their alignment, it's just a matter of doing it at
    >>the right time. Namely, you have to do it after the call to the
    >>DataGrid's DataBind() method. So your code should look something like:
    >>
    >> DataGridID.DataSource = ...
    >> DataGridID.DataBind()
    >>
    >> 'Code to set alignment of columns
    >>
    >>hth
    >>
    >>--
    >>
    >> Scott Mitchell
    >>
    >> http://www.4GuysFromRolla.com
    >>
    >>* When you think ASP.NET, think 4GuysFromRolla.com!
    >>



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 18, 2004
    #4
  5. Jennifer

    Jennifer Guest

    Yes, that is exactly the case. I am stumped. Any suggestions? Is this
    perhaps caused by how I am filling the datagrid? I'm no expert at datagrids
    and my experience with them is limited. I'd love any advice.

    Thank you for all your help!

    Jennifer


    "Scott Mitchell [MVP]" wrote:

    > And if you use the debugger to step through, you see that
    > dtMain.Columns.Count > 1, but the DataGrid's Columns.Count property = 1?
    > That is indeed odd.
    >
    >
    > Jennifer wrote:
    > > Scott,
    > >
    > > Thank you for replying. The columns are being automatically generated. I
    > > do try to set the alignment after the data binding. I'm going to post my
    > > almost-full code below (I'm just not going to show the code for each of the
    > > 61 columns - that would be too much.) Do you have any other suggestions?
    > > Perhaps the way I am setting up the grid in the first place is wrong?
    > >
    > > I appreciate any advice you can give me.
    > >
    > > Thanks,
    > > Jennifer
    > >
    > >
    > > Private Sub FillGrid(ByVal SortExp As String)
    > > Dim dvMain As DataView
    > > Dim X As Int64
    > > Dim dtMain As New DataTable()
    > > Dim DR As DataRow
    > > Dim Update_User As String
    > > If txtStartDate.Text <> "*" Then
    > > If txtEndDate.Text = "*" Then Exit Sub
    > > End If
    > > dvMain = GetDailySales(txtUnitID.Text, txtStartDate.Text,
    > > txtEndDate.Text)
    > > If SortExp <> "" Then
    > > dvMain.Sort = SortExp
    > > End If
    > > Dim Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10 As
    > > String
    > > Dim Col11, Col12, Col13, Col14, Col15, Col16, Col17, Col18, Col19,
    > > Col20 As String
    > > Dim Col21, Col22, Col23, Col24, Col25, Col26, Col27, Col28, Col29,
    > > Col30 As String
    > > Dim Col31, Col32, Col33, Col34, Col35, Col36, Col37, Col38, Col39,
    > > Col40 As String
    > > Dim Col41, Col42, Col43, Col44, Col45, Col46, Col47, Col48, Col49,
    > > Col50, Col51, Col52 As String
    > > Dim Col53, Col54, Col55, Col56, Col57, Col58, Col59, Col60, Col61,
    > > Col62, Col63, Col64 As String
    > >
    > >
    > > Col1 = "Unit"
    > > Col2 = "Date"
    > > Col3 = "Status"
    > > Col4 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$"
    > > Col55 = "Deposit" & vbCrLf & "Evening" & vbCrLf & "$" & vbCrLf &
    > > "Verified"
    > > Col5 = "Deposit" & vbCrLf & "Early_Bird" & vbCrLf & "$"
    > > '-----more column names set here------------
    > >
    > > dtMain.Columns.Add(Col1)
    > > dtMain.Columns.Add(Col2)
    > > dtMain.Columns.Add(Col3)
    > > dtMain.Columns.Add(Col4)
    > > If chkViewVerif.Checked = True Then dtMain.Columns.Add(Col55)
    > > '-----adding the rest of the columns to the data row here
    > >
    > > Dim BD As Date
    > > For X = 0 To dvMain.Count - 1
    > > DR = dtMain.NewRow
    > > DR(Col1) = dvMain(X)("Unit_ID")
    > > BD = dvMain(X)("Business_Date")
    > > DR(Col2) = BD.ToShortDateString
    > > DR(Col3) = dvMain(X)("Status")
    > > If IsDBNull(dvMain(X)("User_ID")) Then
    > > DR(Col50) = dvMain(X)("User_ID")
    > > Else
    > > Update_User = dvMain(X)("User_ID")
    > > DR(Col50) = Update_User.Substring(12)
    > > End If
    > > DR(Col4) = Format(dvMain(X)("Deposit_Eve_Amt"), "N")
    > > If chkViewVerif.Checked = True Then DR(Col55) =
    > > dvMain(X)("Deposit_Eve_Amt_Verified")
    > > '--- assigning the rest of the values here
    > > dtMain.Rows.Add(DR)
    > > Next
    > >
    > > dgDSR.DataSource = dtMain
    > > dgDSR.DataBind()
    > > '---trying to align a column here (and it doesn't work)
    > > dgDSR.Columns(2).ItemStyle.HorizontalAlign = HorizontalAlign.Center
    > >
    > > End Sub
    > >
    > >
    > > "Scott Mitchell [MVP]" wrote:
    > >
    > >
    > >>Jennifer, are you using AutoGenerateColumns=False? If so, you can
    > >>specify the alignment specifically, like so:
    > >>
    > >><asp:BoundColumn ... ItemStyle-HorizontalAlign="Center" ... />
    > >>
    > >>If you are having the column automatically generated, you can
    > >>programmatically set their alignment, it's just a matter of doing it at
    > >>the right time. Namely, you have to do it after the call to the
    > >>DataGrid's DataBind() method. So your code should look something like:
    > >>
    > >> DataGridID.DataSource = ...
    > >> DataGridID.DataBind()
    > >>
    > >> 'Code to set alignment of columns
    > >>
    > >>hth
    > >>
    > >>--
    > >>
    > >> Scott Mitchell
    > >>
    > >> http://www.4GuysFromRolla.com
    > >>
    > >>* When you think ASP.NET, think 4GuysFromRolla.com!
    > >>

    >
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
    >
     
    Jennifer, Nov 19, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmVubmlmZXI=?=

    aligning datagrid columns

    =?Utf-8?B?SmVubmlmZXI=?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,952
    =?Utf-8?B?SmVubmlmZXI=?=
    Nov 22, 2004
  2. =?Utf-8?B?cG11ZA==?=
    Replies:
    2
    Views:
    996
    =?Utf-8?B?cG11ZA==?=
    Jan 10, 2005
  3. Carlos
    Replies:
    0
    Views:
    584
    Carlos
    Aug 14, 2006
  4. Anthony Judd

    Aligning DataGrid Columns!!

    Anthony Judd, Oct 31, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    130
    Scott M.
    Nov 1, 2004
  5. Matthew Van Gundy
    Replies:
    0
    Views:
    112
    Matthew Van Gundy
    Nov 4, 2003
Loading...

Share This Page