Results 1 to 22 of 22

Thread: DataGrid displays DataTable, I want to format date field.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid displays DataTable, I want to format date field.

    Quote 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

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  10. #10
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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:
    1. Dim cn1 As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
    2.                                              & "Persist Security Info=False;" _
    3.                                              & "Initial Catalog=McClure_Web;" _
    4.                                              & "UID=sa;PWD=;" _
    5.                                              & "Data Source=Zeus06")
    6.             cn1.Open()
    7.  
    8.             Dim datAdapt As New OleDb.OleDbDataAdapter("Select [Date], Odometer as [Mileage], " & _
    9.             "Remarks, MaintTotal as [Total Cost], Order_No From Garage_Maint Where Vehicle_ID = " & _
    10.             cmbVehicle.SelectedValue & " Order By [Date] Desc", cn1)
    11.  
    12.             Dim datSet As New Data.DataSet
    13.  
    14.             datAdapt.Fill(datSet, "Garage_Maint")
    15.  
    16.             Dim bcDate As New BoundColumn
    17.  
    18.             With bcDate
    19.                 .DataField = "Date"
    20.                 .DataFormatString = "{0:MM/dd/yyyy}"
    21.                 .HeaderText = "Date"
    22.             End With
    23.             DataGrid1.Columns.Add(bcDate)
    24.  
    25.             DataGrid1.DataSource = datSet
    26.             DataGrid1.DataKeyField = "Order_No"
    27.             DataGrid1.Columns(0).HeaderText = "Select to view Instructions"
    28.             DataGrid1.DataBind()
    29.             cn1.Close()
    30.             cn1.Dispose()
    31.             datadapt.Dispose()
    32.             datSet.Dispose()
    33.             DataGrid1.Visible = True
    34.  
    35. Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
    36.         e.Item.Cells(2).Visible = False
    37.     End Sub
    Thanks
    David Wilhelm

  11. #11
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Re: DataGrid displays DataTable, I want to format date field.

    bump
    David Wilhelm

  12. #12

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  13. #13
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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.
    David Wilhelm

  14. #14

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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>

  15. #15
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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:
    1. 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:
    1. Dim datAdapt As New OleDb.OleDbDataAdapter
    2.         Dim commBuild As New OleDb.OleDbCommandBuilder(datAdapt)
    3.         Dim datSet As New Data.DataSet
    4.         Dim datRow As Data.DataRow
    5.  
    6.         datAdapt = New OleDb.OleDbDataAdapter("Select * From Vacation_Temp Where " & _
    7.         "Vac_EmpNo = '" & cmbEmployee.SelectedValue & "' Order by Vac_Date Desc", cn1)
    8.         commBuild = New OleDb.OleDbCommandBuilder(datAdapt)
    9.         datSet.Clear()
    10.         datAdapt.Fill(datSet, "Vacation_Temp")
    11.  
    12.         Dim bcDate As New BoundColumn
    13.         Dim bcType As New BoundColumn
    14.         Dim bcHours As New BoundColumn
    15.         Dim bcDetails As New BoundColumn
    16.         Dim bcID As New BoundColumn
    17.  
    18.         With bcDate
    19.             .DataField = "Vac_Date"
    20.             .DataFormatString = "{0:MM/dd/yyyy}"
    21.             .HeaderText = "Date"
    22.             .ItemStyle.Width.Equals(60)
    23.         End With
    24.         With bcType
    25.             .DataField = "Vac_Type"
    26.             .HeaderText = "Time Off Type"
    27.             .ItemStyle.Width.Equals(75)
    28.         End With
    29.         With bcHours
    30.             .DataField = "Vac_Hours"
    31.             .HeaderText = "Hours Off"
    32.             .ItemStyle.Width.Equals(50)
    33.         End With
    34.         With bcDetails
    35.             .DataField = "Vac_Type_Details"
    36.             .HeaderText = "Other Vacation Type Details"
    37.             .ItemStyle.Width.Equals(200)
    38.         End With
    39.         With bcID
    40.             .DataField = "ID"
    41.             .Visible = False
    42.         End With
    43.  
    44.         DataGrid1.Columns.Add(bcDate)
    45.         DataGrid1.Columns.Add(bcType)
    46.         DataGrid1.Columns.Add(bcHours)
    47.         DataGrid1.Columns.Add(bcDetails)
    48.         DataGrid1.Columns.Add(bcID)
    49.  
    50.         DataGrid1.DataSource = datSet
    51.         DataGrid1.Columns(0).HeaderText = "Employee # " & cmbEmployee.SelectedValue
    52.         DataGrid1.EditItemIndex = e.Item.ItemIndex
    53.         DataGrid1.DataBind()
    Thank you
    Last edited by indydavid32; Aug 23rd, 2005 at 10:31 AM.
    David Wilhelm

  16. #16

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid displays DataTable, I want to format date field.

    I think it's:
    VB Code:
    1. Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
    2. 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

  17. #17
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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?
    David Wilhelm

  18. #18
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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
    David Wilhelm

  19. #19
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Re: DataGrid displays DataTable, I want to format date field.

    bump
    David Wilhelm

  20. #20

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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:
    1. Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
    2. Dim cbo As DropDownList = CType(DataItem.FindControl("txtDate"), DropDownList)
    3.  
    4. 'set datasource of cbo
    Woof

  21. #21
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    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:
    1. Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
    2. Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
    3.         Dim cbo As DropDownList = CType(DataItem.FindControl("cboType"), DropDownList)
    4.         cbo.Items.Clear()
    5.         cbo.Items.Add("Floating Holiday")
    6.         cbo.Items.Add("Illness")
    7.         cbo.Items.Add("Other Type")
    8.         cbo.Items.Add("Training")
    9.         cbo.Items.Add("Vacation")
    10. End Sub
    Thanks again.
    Last edited by indydavid32; Aug 24th, 2005 at 09:51 AM.
    David Wilhelm

  22. #22

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid displays DataTable, I want to format date field.

    Use DataBound event instead and do:
    VB Code:
    1. Private Sub grdDockets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdDockets.ItemDataBound
    2.     If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    3.             Dim DataItem As DataGridItem = CType(e.Item, DataGridItem)
    4.             Dim cbo As DropDownList = CType(DataItem.FindControl("cboType"), DropDownList)
    5.  
    6.         'add items here
    7.     End If
    8. 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
  •  



Click Here to Expand Forum to Full Width