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
Printable View
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
Change the data formatting expression for the datagrid column to {0:mm/dd/yyyy}
Errr...So that would require me to setup columns 1st?
At the moment I am doing:
So, I am assuming, from the way you suggested I should do:Code:Dim Woof As DataTable = GetMeMyData
DataGrid1.DataSource = Woof
Datagrid1.DataBind
How would I do this?Code:Dim Woof As DataTable = GetMeMyData
'setup columns here
DataGrid1.DataSource = Woof
Datagrid1.DataBind
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
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.
Cheers. Much appreciated.
One last q :D
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
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
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.
Don't suppose you have any code for this?Quote:
Originally Posted by crptcblade
Cheers again, you've been more help tha the contractors here :D
Woka
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...
That should do it.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
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.ThanksVB 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
bump
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
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.
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>
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:
With the new boundcollumns that I create myself, I get the following error with the above code:VB Code:
strDate = Trim(CType(e.Item.Cells(2).Controls(0), TextBox).Text)
Specified argument was out of the range of valid values. Parameter name: index
This is the code I now use to load the datagrid
Thank youVB 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()
I think it's:
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?VB Code:
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem) Dim EnteredDate As String = CType(DataItem.FindControl("txtDate"), TextBox).Text
Woka
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?
Woka, one more thing I need to know. How do you load data into the dropdownlist control made with the itemtemplate?
Thanks again
bump
On the ItemCreated event of the grid you can add some code to populate it.
WoofVB Code:
Dim DataItem As DataGridItem = CType(e.Item, DataGridItem) Dim cbo As DropDownList = CType(DataItem.FindControl("txtDate"), DropDownList) 'set datasource of cbo
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
Here is the vb codeHTML 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>
Thanks again. :)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
Use DataBound event instead and do:
Hope that helps.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
Woka