Text Alignment In DataGrid?


A

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
 
Ad

Advertisements

J

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.
 
D

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"
 
A

Arpan

David, could you please show me an example of formatting using the
style sections?

Arpan
 
A

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
 
Ad

Advertisements

A

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
 
Ad

Advertisements

J

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DataGrid.Columns(Index) 3
Insert New Record 3
Show "No Records Found" Message 1
DB Value in Label 1
DataRow Delete Method 5
Default Selected Item in DropDownList 4
DropDownList DataGrid 1
DataBind 1

Top