|
-
Jan 25th, 2005, 09:20 AM
#1
DataGrid displays DataTable, I want to format date field.
Have returned a datatable from the server.
Have displayed all the data in a datagrid.
I want to format the column "EntryDate" as "mm/dd/yyyy"
Woka
-
Jan 25th, 2005, 09:31 AM
#2
Re: DataGrid displays DataTable, I want to format date field.
Change the data formatting expression for the datagrid column to {0:mm/dd/yyyy}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 25th, 2005, 09:38 AM
#3
Re: DataGrid displays DataTable, I want to format date field.
Errr...So that would require me to setup columns 1st?
At the moment I am doing:
Code:
Dim Woof As DataTable = GetMeMyData
DataGrid1.DataSource = Woof
Datagrid1.DataBind
So, I am assuming, from the way you suggested I should do:
Code:
Dim Woof As DataTable = GetMeMyData
'setup columns here
DataGrid1.DataSource = Woof
Datagrid1.DataBind
How would I do this?
Lets say for example I wanted the following columns:
DataTable FieldName;DataType;Column Heading
So the columns I would like are:
AssetName; String; "Asset"
BookedFrom; Date; "Booked From"
BookedTo; Date; "Booked To"
The DataTable holds other fields, but I want these for other things, I will come to this laterz.
Woka
-
Jan 25th, 2005, 09:48 AM
#4
Re: DataGrid displays DataTable, I want to format date field.
Code:
Dim bcAsset AsNew BoundColumn
Dim bcBookedFrom AsNew BoundColumn
Dim bcBookedTo AsNew BoundColumn
With bcAsset
.DataField = "AssetName"
.HeaderText = "Asset Name"
EndWith
DataGrid1.Columns.Add(bcAsset)
With bcBookedFrom
.DataField = "BookedFrom"
.DataFormatString = "{0:MM/dd/yyyy}"
.HeaderText = "Booked From"
EndWith
DataGrid1.Columns.Add(bcBookedFrom)
With bcBookedTo
.DataField = "BookedTo"
.DataFormatString = "{0:MM/dd/yyyy}"
.HeaderText = "Booked To"
EndWith
DataGrid1.Columns.Add(bcBookedTo)
DataGrid1.DataSource = dt
DataGrid1.DataBind()
Something along those lines.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 25th, 2005, 10:00 AM
#5
Re: DataGrid displays DataTable, I want to format date field.
Cheers. Much appreciated.
One last q 
I have 2 columns:
BookedFrom and BookedTo
These are displayed as:
dd/mm/yyyy hh:nn
Now, can I convert this to one column and show the data as:
dd/mm/yyyy hh:nn to hh:nn
Even though my datatabel is bringing 2 fields back?
ie, is there an event I can trap for when it's going to put something in a column, so I can change what it puts in?
Does that make sense?
Woka
-
Jan 25th, 2005, 10:25 AM
#6
Re: DataGrid displays DataTable, I want to format date field.
Damn...when I add columns in the data gets placed in these columns, but it also add the columns again.
So I have 2 BookedFrom column and 2 BookedTo columns and 2 AssetName column, plus the columns I haven't defined manaually...
Woka
-
Jan 25th, 2005, 10:35 AM
#7
Re: DataGrid displays DataTable, I want to format date field.
Well, for your first question, I always use a template column, and use the Custom binding expression field to work out the display.
For your second question, check the property builder for the grid. Make sure "Generate columns dynamically" (or whatever it is) on the Columns tab is unchecked.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 25th, 2005, 10:39 AM
#8
Re: DataGrid displays DataTable, I want to format date field.
 Originally Posted by crptcblade
Well, for your first question, I always use a template column, and use the Custom binding expression field to work out the display.
Don't suppose you have any code for this?
Cheers again, you've been more help tha the contractors here 
Woka
Last edited by Wokawidget; Jan 25th, 2005 at 10:44 AM.
-
Jan 25th, 2005, 01:14 PM
#9
Re: DataGrid displays DataTable, I want to format date field.
Well, you create a template column in the property builder, and hit OK.
Then right-click and go to Edit Template->Whatever column it is
Add a label to the ItemTemplate portion, select the label, go to the Properties window and choose the Databindings property.
Select Text on the left, and Custom binding expression on the right.
Add this to the text area...
MakeDateRange(DataBinder.Eval(Container.DataItem, "BookedFrom"),DataBinder.Eval(Container.DataItem, "BookedTo"))
Then, go to your code and add this...
Code:
PublicFunction MakeDateRange(ByVal FromDate As DateTime, ByVal ToDate As DateTime) AsString
Return Format(FromDate.Date, "MM/dd/yyyy") & " to " & Format(ToDate.Date, "MM/dd/yyyy")
EndFunction
That should do it.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2005, 10:14 AM
#10
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
crptcblade, thanks for you post I can now format my date field in my datagrid. I do have a question though. Since I add that bound column, the Date field is in there twice. I can easily hide the one I don't want, but that seems a little sloppy to me. Is there a better way? Here's my code.
VB Code:
Dim cn1 As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
& "Persist Security Info=False;" _
& "Initial Catalog=McClure_Web;" _
& "UID=sa;PWD=;" _
& "Data Source=Zeus06")
cn1.Open()
Dim datAdapt As New OleDb.OleDbDataAdapter("Select [Date], Odometer as [Mileage], " & _
"Remarks, MaintTotal as [Total Cost], Order_No From Garage_Maint Where Vehicle_ID = " & _
cmbVehicle.SelectedValue & " Order By [Date] Desc", cn1)
Dim datSet As New Data.DataSet
datAdapt.Fill(datSet, "Garage_Maint")
Dim bcDate As New BoundColumn
With bcDate
.DataField = "Date"
.DataFormatString = "{0:MM/dd/yyyy}"
.HeaderText = "Date"
End With
DataGrid1.Columns.Add(bcDate)
DataGrid1.DataSource = datSet
DataGrid1.DataKeyField = "Order_No"
DataGrid1.Columns(0).HeaderText = "Select to view Instructions"
DataGrid1.DataBind()
cn1.Close()
cn1.Dispose()
datadapt.Dispose()
datSet.Dispose()
DataGrid1.Visible = True
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
e.Item.Cells(2).Visible = False
End Sub
Thanks
-
Aug 22nd, 2005, 12:46 PM
#11
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
-
Aug 22nd, 2005, 02:42 PM
#12
Re: DataGrid displays DataTable, I want to format date field.
turn off auto column generation, and manually create the columns you want.
Right click on the grid and cliuck property builder. Then click columns. YUou can now add bound columns.
Woka
-
Aug 22nd, 2005, 04:03 PM
#13
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
Thanks woka. I will give that a shot. Can you put combo boxes inside an asp datagrid? If so, do you have an example?
Thanks again.
-
Aug 22nd, 2005, 04:53 PM
#14
Re: DataGrid displays DataTable, I want to format date field.
Code:
<Columns>
<asp:TemplateColumn HeaderText="This Is A Template Column">
<ItemTemplate>
<table>
<tr>
<td>
This is a combo:
</td>
<td>
<asp:DropDownList ID="cboItems" Runat="server"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
This is a textbox:
</td>
<td>
<asp:TextBox ID="txtUsername" Runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="A Smaller column">
<ItemTemplate>
<table>
<tr>
<td>
This is a smaller column
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
-
Aug 23rd, 2005, 10:28 AM
#15
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
Woka, thank you for all your help with this. I unchecked "Create columns automatically at run time" and I can now make the boundcollumns I want. I am now having a problem reading from the datagrid.
In the past I would read a cell from the grid with the following code:
VB Code:
strDate = Trim(CType(e.Item.Cells(2).Controls(0), TextBox).Text)
With the new boundcollumns that I create myself, I get the following error with the above code:
Specified argument was out of the range of valid values. Parameter name: index
This is the code I now use to load the datagrid
VB Code:
Dim datAdapt As New OleDb.OleDbDataAdapter
Dim commBuild As New OleDb.OleDbCommandBuilder(datAdapt)
Dim datSet As New Data.DataSet
Dim datRow As Data.DataRow
datAdapt = New OleDb.OleDbDataAdapter("Select * From Vacation_Temp Where " & _
"Vac_EmpNo = '" & cmbEmployee.SelectedValue & "' Order by Vac_Date Desc", cn1)
commBuild = New OleDb.OleDbCommandBuilder(datAdapt)
datSet.Clear()
datAdapt.Fill(datSet, "Vacation_Temp")
Dim bcDate As New BoundColumn
Dim bcType As New BoundColumn
Dim bcHours As New BoundColumn
Dim bcDetails As New BoundColumn
Dim bcID As New BoundColumn
With bcDate
.DataField = "Vac_Date"
.DataFormatString = "{0:MM/dd/yyyy}"
.HeaderText = "Date"
.ItemStyle.Width.Equals(60)
End With
With bcType
.DataField = "Vac_Type"
.HeaderText = "Time Off Type"
.ItemStyle.Width.Equals(75)
End With
With bcHours
.DataField = "Vac_Hours"
.HeaderText = "Hours Off"
.ItemStyle.Width.Equals(50)
End With
With bcDetails
.DataField = "Vac_Type_Details"
.HeaderText = "Other Vacation Type Details"
.ItemStyle.Width.Equals(200)
End With
With bcID
.DataField = "ID"
.Visible = False
End With
DataGrid1.Columns.Add(bcDate)
DataGrid1.Columns.Add(bcType)
DataGrid1.Columns.Add(bcHours)
DataGrid1.Columns.Add(bcDetails)
DataGrid1.Columns.Add(bcID)
DataGrid1.DataSource = datSet
DataGrid1.Columns(0).HeaderText = "Employee # " & cmbEmployee.SelectedValue
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
Thank you
Last edited by indydavid32; Aug 23rd, 2005 at 10:31 AM.
David Wilhelm
-
Aug 23rd, 2005, 10:39 AM
#16
Re: DataGrid displays DataTable, I want to format date field.
I think it's:
VB Code:
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
Dim EnteredDate As String = CType(DataItem.FindControl("txtDate"), TextBox).Text
Just curious, not saying your wrong or anything, but why have you created the columns in VB code (code behind) and not set them up in the ASPX page like my example?
Woka
-
Aug 23rd, 2005, 11:18 AM
#17
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
I just feel more comfortable with vb and not very comfortable with html.
So with my code above, what would I use for txtDate?
If I were to use the template how would I populate the datagrid with my data adapter?
-
Aug 23rd, 2005, 11:51 AM
#18
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
Woka, one more thing I need to know. How do you load data into the dropdownlist control made with the itemtemplate?
Thanks again
-
Aug 24th, 2005, 08:06 AM
#19
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
-
Aug 24th, 2005, 08:50 AM
#20
Re: DataGrid displays DataTable, I want to format date field.
On the ItemCreated event of the grid you can add some code to populate it.
VB Code:
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
Dim cbo As DropDownList = CType(DataItem.FindControl("txtDate"), DropDownList)
'set datasource of cbo
Woof
-
Aug 24th, 2005, 09:45 AM
#21
Fanatic Member
Re: DataGrid displays DataTable, I want to format date field.
Woka, I am not getting this data from a table. I tried the following, but I recevied an error that said "Object reference not set to an instance of an object.".
Here is the html code
HTML Code:
<asp:TemplateColumn HeaderText="Time Off Type">
<ItemTemplate>
<table>
<tr>
<td>
<asp:DropDownList ID="cboType" Runat="server" width="100"></asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
Here is the vb code
VB Code:
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
Dim cbo As DropDownList = CType(DataItem.FindControl("cboType"), DropDownList)
cbo.Items.Clear()
cbo.Items.Add("Floating Holiday")
cbo.Items.Add("Illness")
cbo.Items.Add("Other Type")
cbo.Items.Add("Training")
cbo.Items.Add("Vacation")
End Sub
Thanks again.
Last edited by indydavid32; Aug 24th, 2005 at 09:51 AM.
David Wilhelm
-
Aug 24th, 2005, 10:17 AM
#22
Re: DataGrid displays DataTable, I want to format date field.
Use DataBound event instead and do:
VB Code:
Private Sub grdDockets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdDockets.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
Dim cbo As DropDownList = CType(DataItem.FindControl("cboType"), DropDownList)
'add items here
End If
End Sub
Hope that helps.
Woka
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|