By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Text Alignment In DataGrid?

P: n/a
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

Aug 31 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
off the top of my head in your load event after binding its something like:

dgMarks.Columns(0).ItemStyle.HorizontalAlign=Horiz ontalAlign.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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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

Aug 31 '06 #2

P: n/a
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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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

Aug 31 '06 #3

P: n/a
David, could you please show me an example of formatting using the
style sections?

Arpan
David Wier wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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
Aug 31 '06 #4

P: n/a
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=Horiz ontalAlign.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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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
Aug 31 '06 #5

P: n/a
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/lib...itemstyle.aspx
http://msdn2.microsoft.com/en-us/lib...d.columns.aspx

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

John Timney (MVP)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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=Hori zontalAlign.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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegr oups.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

Aug 31 '06 #6

P: n/a
John, the problem in the code that I have shown in post #1 doesn't use
the <Columnscollection 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 <Columnscollection (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/lib...itemstyle.aspx
http://msdn2.microsoft.com/en-us/lib...d.columns.aspx

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

John Timney (MVP)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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=Horiz ontalAlign.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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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
Aug 31 '06 #7

P: n/a
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)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
John, the problem in the code that I have shown in post #1 doesn't use
the <Columnscollection 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 <Columnscollection (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/lib...itemstyle.aspx
http://msdn2.microsoft.com/en-us/lib...d.columns.aspx

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

John Timney (MVP)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegr oups.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=Hori zontalAlign.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" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegr oups.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


Sep 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.