Home All Groups Group Topic Archive Search About

Text Alignment In DataGrid?

Author
31 Aug 2006 7:49 PM
Arpan
Assume that a database table has the following 4 columns - ID, UserID,
Subject & Marks. I am retrieving the records existing in this DB table
& displaying them in a DataGrid like this:

<script runat="server">
    Sub Page_Load(obj As Object, ea As EventArgs)
        Dim dSet As DataSet
        Dim sqlConn As SqlConnection
        Dim sqlDapter As SqlDataAdapter

        sqlConn = New SqlConnection("........")
        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
sqlConn)
        dSet = New DataSet

        sqlDapter.Fill(dSet, "Marks")

        dgMarks.DataSource = dSet
        dgMarks.DataBind()
    End Sub
</script>
<form eunat="server">
<asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
HeaderStyle-HorizontalAlign="center" runat="server"/>
</form>

The above will retrieve & display all the records existing in the 4
columns in the DataGrid. By default all the records in the different
cells in the DataGrid will be left-aligned. Now I want only the records
existing under the ID column to be right-aligned & the records existing
under the Marks column to be center-aligned. The records under UserID &
Subject columns should remain left-aligned.

How do I accomplish this?

Thanks,

Arpan

Author
31 Aug 2006 8:24 PM
John Timney (MVP)
off the top of my head in your load event after binding its something like:

dgMarks.Columns(0).ItemStyle.HorizontalAlign=HorizontalAlign.Center

or  declared it would probably be something like this
<ItemStyle Width="150px" VerticalAlign="middle" HorizontalAlign="right" />

You might have to pick it apart a bit, its not tested.
--
--
Regards

John Timney (MVP)


Show quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
> Assume that a database table has the following 4 columns - ID, UserID,
> Subject & Marks. I am retrieving the records existing in this DB table
> & displaying them in a DataGrid like this:
>
> <script runat="server">
>    Sub Page_Load(obj As Object, ea As EventArgs)
>        Dim dSet As DataSet
>        Dim sqlConn As SqlConnection
>        Dim sqlDapter As SqlDataAdapter
>
>        sqlConn = New SqlConnection("........")
>        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
> sqlConn)
>        dSet = New DataSet
>
>        sqlDapter.Fill(dSet, "Marks")
>
>        dgMarks.DataSource = dSet
>        dgMarks.DataBind()
>    End Sub
> </script>
> <form eunat="server">
> <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
> HeaderStyle-HorizontalAlign="center" runat="server"/>
> </form>
>
> The above will retrieve & display all the records existing in the 4
> columns in the DataGrid. By default all the records in the different
> cells in the DataGrid will be left-aligned. Now I want only the records
> existing under the ID column to be right-aligned & the records existing
> under the Marks column to be center-aligned. The records under UserID &
> Subject columns should remain left-aligned.
>
> How do I accomplish this?
>
> Thanks,
>
> Arpan
>
Author
31 Aug 2006 9:46 PM
Arpan
John, using

dgMarks.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right

after binding the data to the DataGrid generates the following error:

Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

pointing to the above line. Even the code

Response.Write(dgMarks.Columns.Count)

evaluates to 0! If the ItemStyle line is removed, then I can see the
DataGrid displaying the 4 columns - ID, UserID, Subject & Marks. So how
come the Columns count property evaluates to 0?

Thanks,

Regards,

Arpan

John Timney (MVP) wrote:
Show quote
> off the top of my head in your load event after binding its something like:
>
> dgMarks.Columns(0).ItemStyle.HorizontalAlign=HorizontalAlign.Center
>
> or  declared it would probably be something like this
> <ItemStyle Width="150px" VerticalAlign="middle" HorizontalAlign="right" />
>
> You might have to pick it apart a bit, its not tested.
> --
> --
> Regards
>
> John Timney (MVP)
>
>
> "Arpan" <arpan***@hotmail.com> wrote in message
> news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
> > Assume that a database table has the following 4 columns - ID, UserID,
> > Subject & Marks. I am retrieving the records existing in this DB table
> > & displaying them in a DataGrid like this:
> >
> > <script runat="server">
> >    Sub Page_Load(obj As Object, ea As EventArgs)
> >        Dim dSet As DataSet
> >        Dim sqlConn As SqlConnection
> >        Dim sqlDapter As SqlDataAdapter
> >
> >        sqlConn = New SqlConnection("........")
> >        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
> > sqlConn)
> >        dSet = New DataSet
> >
> >        sqlDapter.Fill(dSet, "Marks")
> >
> >        dgMarks.DataSource = dSet
> >        dgMarks.DataBind()
> >    End Sub
> > </script>
> > <form eunat="server">
> > <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
> > HeaderStyle-HorizontalAlign="center" runat="server"/>
> > </form>
> >
> > The above will retrieve & display all the records existing in the 4
> > columns in the DataGrid. By default all the records in the different
> > cells in the DataGrid will be left-aligned. Now I want only the records
> > existing under the ID column to be right-aligned & the records existing
> > under the Marks column to be center-aligned. The records under UserID &
> > Subject columns should remain left-aligned.
> >
> > How do I accomplish this?
> >
> > Thanks,
> >
> > Arpan
> >
Author
31 Aug 2006 10:12 PM
John Timney (MVP)
Sorry Arpan,

I dont have a working environment in front of me.  Here are a couple of
links that will help you get your example working by declaring the
<ItemStyle>

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagridcolumn.itemstyle.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.columns.aspx

Either way, you'll likely need to set the columns up for either approach to
work.
--
--
Regards

John Timney (MVP)


Show quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1157060770.953767.42580@m73g2000cwd.googlegroups.com...
> John, using
>
> dgMarks.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
>
> after binding the data to the DataGrid generates the following error:
>
> Index was out of range. Must be non-negative and less than the size of
> the collection.
> Parameter name: index
>
> pointing to the above line. Even the code
>
> Response.Write(dgMarks.Columns.Count)
>
> evaluates to 0! If the ItemStyle line is removed, then I can see the
> DataGrid displaying the 4 columns - ID, UserID, Subject & Marks. So how
> come the Columns count property evaluates to 0?
>
> Thanks,
>
> Regards,
>
> Arpan
>
> John Timney (MVP) wrote:
>> off the top of my head in your load event after binding its something
>> like:
>>
>> dgMarks.Columns(0).ItemStyle.HorizontalAlign=HorizontalAlign.Center
>>
>> or  declared it would probably be something like this
>> <ItemStyle Width="150px" VerticalAlign="middle" HorizontalAlign="right"
>> />
>>
>> You might have to pick it apart a bit, its not tested.
>> --
>> --
>> Regards
>>
>> John Timney (MVP)
>>
>>
>> "Arpan" <arpan***@hotmail.com> wrote in message
>> news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
>> > Assume that a database table has the following 4 columns - ID, UserID,
>> > Subject & Marks. I am retrieving the records existing in this DB table
>> > & displaying them in a DataGrid like this:
>> >
>> > <script runat="server">
>> >    Sub Page_Load(obj As Object, ea As EventArgs)
>> >        Dim dSet As DataSet
>> >        Dim sqlConn As SqlConnection
>> >        Dim sqlDapter As SqlDataAdapter
>> >
>> >        sqlConn = New SqlConnection("........")
>> >        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
>> > sqlConn)
>> >        dSet = New DataSet
>> >
>> >        sqlDapter.Fill(dSet, "Marks")
>> >
>> >        dgMarks.DataSource = dSet
>> >        dgMarks.DataBind()
>> >    End Sub
>> > </script>
>> > <form eunat="server">
>> > <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
>> > HeaderStyle-HorizontalAlign="center" runat="server"/>
>> > </form>
>> >
>> > The above will retrieve & display all the records existing in the 4
>> > columns in the DataGrid. By default all the records in the different
>> > cells in the DataGrid will be left-aligned. Now I want only the records
>> > existing under the ID column to be right-aligned & the records existing
>> > under the Marks column to be center-aligned. The records under UserID &
>> > Subject columns should remain left-aligned.
>> >
>> > How do I accomplish this?
>> >
>> > Thanks,
>> >
>> > Arpan
>> >
>
Author
31 Aug 2006 11:21 PM
Arpan
John, the problem in the code that I have shown in post #1 doesn't use
the <Columns> collection to populate the DataGrid i.e. the 4 columns
displayed in the DataGrid are generated automatically which why the
Columns count property evaluates to 0 but the examples shown in the 2
URLs you have referred use the <Columns> collection (like BoundColumn,
ButtonColumn, TemplateColumn) explicitly to render the DataGrid. They
don't show how to format records under auto-generated columns.

Arpan


John Timney (MVP) wrote:
Show quote
> Sorry Arpan,
>
> I dont have a working environment in front of me.  Here are a couple of
> links that will help you get your example working by declaring the
> <ItemStyle>
>
> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagridcolumn.itemstyle.aspx
> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.columns.aspx
>
> Either way, you'll likely need to set the columns up for either approach to
> work.
> --
> --
> Regards
>
> John Timney (MVP)
>
>
> "Arpan" <arpan***@hotmail.com> wrote in message
> news:1157060770.953767.42580@m73g2000cwd.googlegroups.com...
> > John, using
> >
> > dgMarks.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
> >
> > after binding the data to the DataGrid generates the following error:
> >
> > Index was out of range. Must be non-negative and less than the size of
> > the collection.
> > Parameter name: index
> >
> > pointing to the above line. Even the code
> >
> > Response.Write(dgMarks.Columns.Count)
> >
> > evaluates to 0! If the ItemStyle line is removed, then I can see the
> > DataGrid displaying the 4 columns - ID, UserID, Subject & Marks. So how
> > come the Columns count property evaluates to 0?
> >
> > Thanks,
> >
> > Regards,
> >
> > Arpan
> >
> > John Timney (MVP) wrote:
> >> off the top of my head in your load event after binding its something
> >> like:
> >>
> >> dgMarks.Columns(0).ItemStyle.HorizontalAlign=HorizontalAlign.Center
> >>
> >> or  declared it would probably be something like this
> >> <ItemStyle Width="150px" VerticalAlign="middle" HorizontalAlign="right"
> >> />
> >>
> >> You might have to pick it apart a bit, its not tested.
> >> --
> >> --
> >> Regards
> >>
> >> John Timney (MVP)
> >>
> >>
> >> "Arpan" <arpan***@hotmail.com> wrote in message
> >> news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
> >> > Assume that a database table has the following 4 columns - ID, UserID,
> >> > Subject & Marks. I am retrieving the records existing in this DB table
> >> > & displaying them in a DataGrid like this:
> >> >
> >> > <script runat="server">
> >> >    Sub Page_Load(obj As Object, ea As EventArgs)
> >> >        Dim dSet As DataSet
> >> >        Dim sqlConn As SqlConnection
> >> >        Dim sqlDapter As SqlDataAdapter
> >> >
> >> >        sqlConn = New SqlConnection("........")
> >> >        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
> >> > sqlConn)
> >> >        dSet = New DataSet
> >> >
> >> >        sqlDapter.Fill(dSet, "Marks")
> >> >
> >> >        dgMarks.DataSource = dSet
> >> >        dgMarks.DataBind()
> >> >    End Sub
> >> > </script>
> >> > <form eunat="server">
> >> > <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
> >> > HeaderStyle-HorizontalAlign="center" runat="server"/>
> >> > </form>
> >> >
> >> > The above will retrieve & display all the records existing in the 4
> >> > columns in the DataGrid. By default all the records in the different
> >> > cells in the DataGrid will be left-aligned. Now I want only the records
> >> > existing under the ID column to be right-aligned & the records existing
> >> > under the Marks column to be center-aligned. The records under UserID &
> >> > Subject columns should remain left-aligned.
> >> >
> >> > How do I accomplish this?
> >> >
> >> > Thanks,
> >> >
> >> > Arpan
> >> >
> >
Author
1 Sep 2006 8:02 AM
John Timney (MVP)
I suspect you will actually have to use the columns collection given your
trying to set an individual column as opposed to the whole grid.

--
Regards

John Timney (MVP)


Show quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1157066507.486867.63010@p79g2000cwp.googlegroups.com...
> John, the problem in the code that I have shown in post #1 doesn't use
> the <Columns> collection to populate the DataGrid i.e. the 4 columns
> displayed in the DataGrid are generated automatically which why the
> Columns count property evaluates to 0 but the examples shown in the 2
> URLs you have referred use the <Columns> collection (like BoundColumn,
> ButtonColumn, TemplateColumn) explicitly to render the DataGrid. They
> don't show how to format records under auto-generated columns.
>
> Arpan
>
>
> John Timney (MVP) wrote:
>> Sorry Arpan,
>>
>> I dont have a working environment in front of me.  Here are a couple of
>> links that will help you get your example working by declaring the
>> <ItemStyle>
>>
>> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagridcolumn.itemstyle.aspx
>> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.columns.aspx
>>
>> Either way, you'll likely need to set the columns up for either approach
>> to
>> work.
>> --
>> --
>> Regards
>>
>> John Timney (MVP)
>>
>>
>> "Arpan" <arpan***@hotmail.com> wrote in message
>> news:1157060770.953767.42580@m73g2000cwd.googlegroups.com...
>> > John, using
>> >
>> > dgMarks.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
>> >
>> > after binding the data to the DataGrid generates the following error:
>> >
>> > Index was out of range. Must be non-negative and less than the size of
>> > the collection.
>> > Parameter name: index
>> >
>> > pointing to the above line. Even the code
>> >
>> > Response.Write(dgMarks.Columns.Count)
>> >
>> > evaluates to 0! If the ItemStyle line is removed, then I can see the
>> > DataGrid displaying the 4 columns - ID, UserID, Subject & Marks. So how
>> > come the Columns count property evaluates to 0?
>> >
>> > Thanks,
>> >
>> > Regards,
>> >
>> > Arpan
>> >
>> > John Timney (MVP) wrote:
>> >> off the top of my head in your load event after binding its something
>> >> like:
>> >>
>> >> dgMarks.Columns(0).ItemStyle.HorizontalAlign=HorizontalAlign.Center
>> >>
>> >> or  declared it would probably be something like this
>> >> <ItemStyle Width="150px" VerticalAlign="middle"
>> >> HorizontalAlign="right"
>> >> />
>> >>
>> >> You might have to pick it apart a bit, its not tested.
>> >> --
>> >> --
>> >> Regards
>> >>
>> >> John Timney (MVP)
>> >>
>> >>
>> >> "Arpan" <arpan***@hotmail.com> wrote in message
>> >> news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
>> >> > Assume that a database table has the following 4 columns - ID,
>> >> > UserID,
>> >> > Subject & Marks. I am retrieving the records existing in this DB
>> >> > table
>> >> > & displaying them in a DataGrid like this:
>> >> >
>> >> > <script runat="server">
>> >> >    Sub Page_Load(obj As Object, ea As EventArgs)
>> >> >        Dim dSet As DataSet
>> >> >        Dim sqlConn As SqlConnection
>> >> >        Dim sqlDapter As SqlDataAdapter
>> >> >
>> >> >        sqlConn = New SqlConnection("........")
>> >> >        sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
>> >> > sqlConn)
>> >> >        dSet = New DataSet
>> >> >
>> >> >        sqlDapter.Fill(dSet, "Marks")
>> >> >
>> >> >        dgMarks.DataSource = dSet
>> >> >        dgMarks.DataBind()
>> >> >    End Sub
>> >> > </script>
>> >> > <form eunat="server">
>> >> > <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
>> >> > HeaderStyle-HorizontalAlign="center" runat="server"/>
>> >> > </form>
>> >> >
>> >> > The above will retrieve & display all the records existing in the 4
>> >> > columns in the DataGrid. By default all the records in the different
>> >> > cells in the DataGrid will be left-aligned. Now I want only the
>> >> > records
>> >> > existing under the ID column to be right-aligned & the records
>> >> > existing
>> >> > under the Marks column to be center-aligned. The records under
>> >> > UserID &
>> >> > Subject columns should remain left-aligned.
>> >> >
>> >> > How do I accomplish this?
>> >> >
>> >> > Thanks,
>> >> >
>> >> > Arpan
>> >> >
>> >
>
Author
31 Aug 2006 9:01 PM
David Wier
Any formatting can be done through the style sections -
ItemStyle/HeaderStyle/FooterStyle, etc - with the actual records, you'd use
ItemStyle-HorizontalAlign="right"

Show quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
> Assume that a database table has the following 4 columns - ID, UserID,
> Subject & Marks. I am retrieving the records existing in this DB table
> & displaying them in a DataGrid like this:
>
> <script runat="server">
>     Sub Page_Load(obj As Object, ea As EventArgs)
>         Dim dSet As DataSet
>         Dim sqlConn As SqlConnection
>         Dim sqlDapter As SqlDataAdapter
>
>         sqlConn = New SqlConnection("........")
>         sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
> sqlConn)
>         dSet = New DataSet
>
>         sqlDapter.Fill(dSet, "Marks")
>
>         dgMarks.DataSource = dSet
>         dgMarks.DataBind()
>     End Sub
> </script>
> <form eunat="server">
> <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
> HeaderStyle-HorizontalAlign="center" runat="server"/>
> </form>
>
> The above will retrieve & display all the records existing in the 4
> columns in the DataGrid. By default all the records in the different
> cells in the DataGrid will be left-aligned. Now I want only the records
> existing under the ID column to be right-aligned & the records existing
> under the Marks column to be center-aligned. The records under UserID &
> Subject columns should remain left-aligned.
>
> How do I accomplish this?
>
> Thanks,
>
> Arpan
>
Author
31 Aug 2006 9:27 PM
Arpan
David, could you please show me an example of formatting using the
style sections?

Arpan


David Wier wrote:
Show quote
> Any formatting can be done through the style sections -
> ItemStyle/HeaderStyle/FooterStyle, etc - with the actual records, you'd use
> ItemStyle-HorizontalAlign="right"
>
> --
> David Wier
> MVP/ASPInsider
> http://aspnet101.com
> http://aspexpress.com
>
>
> "Arpan" <arpan***@hotmail.com> wrote in message
> news:1157053789.598649.94470@i42g2000cwa.googlegroups.com...
> > Assume that a database table has the following 4 columns - ID, UserID,
> > Subject & Marks. I am retrieving the records existing in this DB table
> > & displaying them in a DataGrid like this:
> >
> > <script runat="server">
> >     Sub Page_Load(obj As Object, ea As EventArgs)
> >         Dim dSet As DataSet
> >         Dim sqlConn As SqlConnection
> >         Dim sqlDapter As SqlDataAdapter
> >
> >         sqlConn = New SqlConnection("........")
> >         sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
> > sqlConn)
> >         dSet = New DataSet
> >
> >         sqlDapter.Fill(dSet, "Marks")
> >
> >         dgMarks.DataSource = dSet
> >         dgMarks.DataBind()
> >     End Sub
> > </script>
> > <form eunat="server">
> > <asp:DataGrid ID="dgMarks" HeaderStyle-Font-Bold="true"
> > HeaderStyle-HorizontalAlign="center" runat="server"/>
> > </form>
> >
> > The above will retrieve & display all the records existing in the 4
> > columns in the DataGrid. By default all the records in the different
> > cells in the DataGrid will be left-aligned. Now I want only the records
> > existing under the ID column to be right-aligned & the records existing
> > under the Marks column to be center-aligned. The records under UserID &
> > Subject columns should remain left-aligned.
> >
> > How do I accomplish this?
> >
> > Thanks,
> >
> > Arpan
> >

AddThis Social Bookmark Button