all groups > vb.net controls > july 2004 >
You're in the

vb.net controls

group:

Formatting an ASP datagrid using VB.Net code-behind


Formatting an ASP datagrid using VB.Net code-behind Coleen
7/27/2004 1:26:24 PM
vb.net controls:
Hi all :-)

I'm back to trying to format my ASP. Net datagrids...I can format the =
totals row easily enough by doing a count -1 in the VB code behind. =
What I need to do is for each row, I need to set the alignment of each =
cell to either left or right. In my for loop I have code to add the =
row:

dt_stat_report_5.Rows.Add(dr_stat_report_5)

Now for each row I need to set the alignment...can anyone please help me =
with this? I've tried using:

dt_stat_report_5.Rows.Items(0).HorizontalAlign.Left - but of course =
this does not work, there is not HorizontalAlign available. Can someone =
please tell me a way to format the alignment in the VB Code-behind? TIA
Re: Formatting an ASP datagrid using VB.Net code-behind Jared
7/29/2004 7:25:36 AM
Coleen,
I think you have to make the change in the ItemDataBound event. Try
something like this:
Jared

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then LoadData()
End Sub

Private Sub LoadData()
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString =
"Server=localhost;Database=Northwind;Trusted_Connection=True;"
conn.Open()
Dim da As New SqlClient.SqlDataAdapter("Select TitleOfCourtesy,
FirstName, LastName, Title FROM Employees Order By FirstName", conn)
Dim ds As New DataSet
da.Fill(ds)
conn.Close()
Me.DataGrid1.DataSource = ds
Me.DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If Not e.Item.DataItem Is Nothing Then
If e.Item.ItemType = ListItemType.AlternatingItem Or
ListItemType.Item Or ListItemType.SelectedItem Then
Dim Item As DataGridItem = CType(e.Item, DataGridItem)
With Item
.Cells(0).HorizontalAlign = HorizontalAlign.Right
End With
End If
End If
End Sub

[quoted text, click to view]
Hi all :-)

I'm back to trying to format my ASP. Net datagrids...I can format the totals
row easily enough by doing a count -1 in the VB code behind. What I need to
do is for each row, I need to set the alignment of each cell to either left
or right. In my for loop I have code to add the row:

dt_stat_report_5.Rows.Add(dr_stat_report_5)

Now for each row I need to set the alignment...can anyone please help me
with this? I've tried using:

dt_stat_report_5.Rows.Items(0).HorizontalAlign.Left - but of course this
does not work, there is not HorizontalAlign available. Can someone please
tell me a way to format the alignment in the VB Code-behind? TIA
Coleen

Re: Formatting an ASP datagrid using VB.Net code-behind Coleen
7/29/2004 12:22:09 PM
Oh, okay that was not a way I had considered. I will try that. We connect
to a DB2 Database, not an SQL Database, so I'd forgotten about the With
event. Thanks!
With Item
.Cells(0).HorizontalAlign = HorizontalAlign.Right
End With


[quoted text, click to view]

Re: Formatting an ASP datagrid using VB.Net code-behind Coleen
7/29/2004 2:18:18 PM
A great big THANK YOU! to you Jared! This is SO much easier than how I was
trying to do this...

Coleen

[quoted text, click to view]

AddThis Social Bookmark Button