How do I format fields in a dynamic datagrid/dataview?

liunx

Guest
I know how to format a normal datagrid, but what I want to know is, how to format columns that I get dynamically into a dataview. Here's my code:
(conn is a connection object I placed in the design view)
I've declared these as global:

Dim ds As DataSet = New DataSet
Dim dv As DataView

Then in my bind() sub...

Private Sub bind()
Try
txtQry.Text = Session("selCmd")
Dim da As New SqlClient.SqlDataAdapter(Session("selCmd") + Session("sortCmd"), conn)
da.Fill(ds)
dv = New DataView(ds.Tables(0))
DataGrid1.DataSource = dv
DataBind()
Catch ex As Exception
Response.Write("<div style=""BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 12px; BORDER-LEFT: black 1px solid; COLOR: #ffff99; BORDER-BOTTOM: black 1px solid; FONT-STYLE: normal; FONT-FAMILY: Verdana; BACKGROUND-COLOR: firebrick"">DEBUG ERROR (SELECT MESSAGE): " + Session("selCmd").ToString + "</div>")
End Try
End Sub

txtQry.Text actually gets its' input from a query builder. Session("selCmd") holds the actual query, and Session("selSort") holds the sort order for the datagrid paging/sorting. To use the tried and true Northwind example... if the query has "SELECT * FROM ORDERS", there's a datetime and money field. (the date might look something like 01/01/1899 12:38pm). I want to parse this so that it says "12:38pm". What I want to know is how to do it in a dynamic datagrid/view like this..

EDIT: I tried something like this:

Dim i, j As Integer
' Go through each column...
For i = 0 To (dv.Table.Columns.Count - 1)
If dv.Table.Columns(i).DataType.Name.ToString = "DateTime" Then
For j = 0 To (dv.Table.Rows.Count - 1)
If IsDate(dv.Table.Rows(j).Item(i)) Then
dv.Table.Rows(j).Item(i) = CDate("12/12/2004").ToShortDateString 'CDate(dv.Table.Rows(j).Item(i).ToString).ToShortDateString
End If
Next
End If
Next

But the date is still showing as a longdatestring. (i.e. 12/12/2004 12:00:00 AM). Note: I used "12/12/2004" to make sure it was getting to that line of code.

EDIT: It seems the datagrid is over-riding whatever I put into the dataview, but I can't seem to find how to edit the datagrid. (datagrid1.items(0).cells(0).text = "...", for example, doesn't seem to work)
 
Back
Top