suran78
asked on
Date calender in Datagrid
Experts,
I have a field in datagrid that displays date in a textbox from sql server table. I need to change this to a calender. So that the user can enter date in the textbox and also has the option to click on small dropdown arrow beside the text box to open a calender control( just like in airline websites). How can I code this ? Also when the user opens the calender the date retrieved from SQL server table should be selected if it's not null. My datagrid looks like this. Please give me the asp.net and VB.net code.
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server" CellPadding="2" EditItemStyle-BackColor="y ellow"
DataKeyField="Block_no" OnEditCommand="DoItemEdit" OnUpdateCommand="DoItemUpd ate" OnCancelCommand="DoItemCan cel" AutoGenerateColumns="False " style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 80px">
<EditItemStyle BackColor="Yellow"></EditI temStyle>
<Columns>
<asp:BoundColumn DataField="Block_no" ReadOnly="True" HeaderText="Block_no"></as p:BoundCol umn>
<asp:TemplateColumn HeaderText="series_no">
<ItemTemplate>
<ASP:Label Text='<%# Container.DataItem("series _no") %>' runat="server" ID="Label1"/>
</ItemTemplate>
<EditItemTemplate>
<ASP:TextBox id="txtTitle" Text='<%# Container.DataItem("Series _no") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="erection_date" HeaderText="start Date"></asp:BoundColumn>
<asp:BoundColumn DataField="disabled_date" HeaderText="Disabled Date"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:Edit CommandCol umn>
</Columns>
</ASP:DataGrid>
I have a field in datagrid that displays date in a textbox from sql server table. I need to change this to a calender. So that the user can enter date in the textbox and also has the option to click on small dropdown arrow beside the text box to open a calender control( just like in airline websites). How can I code this ? Also when the user opens the calender the date retrieved from SQL server table should be selected if it's not null. My datagrid looks like this. Please give me the asp.net and VB.net code.
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server" CellPadding="2" EditItemStyle-BackColor="y
DataKeyField="Block_no" OnEditCommand="DoItemEdit"
<EditItemStyle BackColor="Yellow"></EditI
<Columns>
<asp:BoundColumn DataField="Block_no" ReadOnly="True" HeaderText="Block_no"></as
<asp:TemplateColumn HeaderText="series_no">
<ItemTemplate>
<ASP:Label Text='<%# Container.DataItem("series
</ItemTemplate>
<EditItemTemplate>
<ASP:TextBox id="txtTitle" Text='<%# Container.DataItem("Series
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="erection_date" HeaderText="start Date"></asp:BoundColumn>
<asp:BoundColumn DataField="disabled_date" HeaderText="Disabled Date"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:Edit
</Columns>
</ASP:DataGrid>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
replace the line you get the error by this one:
objPubDateCtrl = CType(MyDataGrid.Items(obj Args.Item. ItemIndex) .Cells(CEL L_INDEX).F indControl ("txtPubDa te"), TextBox)
replace CELL_INDEX with the proper index of that datagrid column
objPubDateCtrl = CType(MyDataGrid.Items(obj
replace CELL_INDEX with the proper index of that datagrid column
ASKER
I di dso not working, even though line 65 has nothing it is giving the same cast error at line 65:
Line 63:
Line 64: Dim objTitleCtrl As TextBox = CType(e.Item.FindControl(" txtTitle") , TextBox)
Line 65: ERROR AT THIS LINE!!!!
Line 66: Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I tem.ItemIn dex).Cells (4).FindCo ntrol("txt PubDate"), TextBox)
Line 67:
The erection date is of type Date in sql database. It that creating this error ?
Line 63:
Line 64: Dim objTitleCtrl As TextBox = CType(e.Item.FindControl("
Line 65: ERROR AT THIS LINE!!!!
Line 66: Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I
Line 67:
The erection date is of type Date in sql database. It that creating this error ?
ASKER
I have 5 columns in datagrid,
Block Series Patch Erection_date Disabled
Is 4 the correct CELL_INDEX for objPubDateCtrl, if erection_date refers to objPubDateCtrl ? Please reply.
Block Series Patch Erection_date Disabled
Is 4 the correct CELL_INDEX for objPubDateCtrl, if erection_date refers to objPubDateCtrl ? Please reply.
no, the column index is zero based, so its 3
suppress that line an put it this way, also change the column index to 3:
Dim objTitleCtrl As TextBox = CType(e.Item.FindControl(" txtTitle") , TextBox)
Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I tem.ItemIn dex).Cells (3).FindCo ntrol("txt PubDate"), TextBox)
Dim objTitleCtrl As TextBox = CType(e.Item.FindControl("
Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I
ASKER
I deleted that lie and pasted. I don't know what is wrog with line 65. Maybe the error is somewhere else. Please advise what i should do ?
Line 63:
Line 64: Dim objTitleCtrl As TextBox = CType(e.Item.FindControl(" txtPatch") , TextBox)
Line 65: Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I tem.ItemIn dex).Cells (3).FindCo ntrol("txt PubDate"), TextBox) <-----------------Error at line 65
Line 66:
Line 67:
Line 63:
Line 64: Dim objTitleCtrl As TextBox = CType(e.Item.FindControl("
Line 65: Dim objPubDateCtrl As TextBox = CType(MyDataGrid.Items(e.I
Line 66:
Line 67:
ASKER
Tried that already. This is strange but I have to say that line 65 is always giving error, maybe the probelm is somewhere else. I cut and pasted the whole sub to some other place in aspx.vb file. But line 65 is still giving error. Even if I keep it empty it's still giving error. Please help I don't know how to solve this problem. I am very puzzled.
what is the complete error message?
ASKER
I solved it. The filename was datagrid.aspx. It created a public classl called datagrid which is a reserved word. So I was getting this funny error which had nothing to do with cast.
I have not yet implemented the calender popup. Not closing this question right now. Thanks for hanging on.
I have not yet implemented the calender popup. Not closing this question right now. Thanks for hanging on.
also paste the hole html datagrid definition
:)
ASKER
Thanks for your help. I found another calender pop up which is very useful too, they also have other tools like time picker which i will be using in my application. You might want to try it too.
The website is:
http://www.eworldui.net/Articles/Installation.aspx
The website is:
http://www.eworldui.net/Articles/Installation.aspx
thanks
ASKER
I am opened another question on this calender pop up. It's unable to edit a null date in datagrid. Please reply if you can help with the calendar pop up.
ASKER
According to Eccentric support this is how it should be done. But I don't know how and where to put this code.
You need to check the value coming back from the database prior to setting it to
SelectedDate. Just wrap things like:
if(value == DBNull.Value) {
return DateTime.MinValue;
} else {
return value;
}
You need to check the value coming back from the database prior to setting it to
SelectedDate. Just wrap things like:
if(value == DBNull.Value) {
return DateTime.MinValue;
} else {
return value;
}
i see, you have to set up the SelectedDate property to call a function (ie SetDate) and pass the current value to it to be processed:
SelectedDate='<%# SetDate(Container.DataItem ("disabled _date")) %>'
then in the codebehind define the fucntion like this:
Friend Function SetDate(theDate as Object)
If theDate= DBNull.Value Then
Return DateTime.MinValue;
Else
Return theDate
End If
End Function
SelectedDate='<%# SetDate(Container.DataItem
then in the codebehind define the fucntion like this:
Friend Function SetDate(theDate as Object)
If theDate= DBNull.Value Then
Return DateTime.MinValue;
Else
Return theDate
End If
End Function
ASKER
Exception Details: System.InvalidCastExceptio
<asp:TemplateColumn HeaderText="Erection_date"
<ItemTemplate>
<ASP:Label Text='<%# Container.DataItem("start_
</ItemTemplate>
<EditItemTemplate>
<ASP:Textbox id="txtPubDate" runat="server" Text='<%# Container.DataItem("Erecti
</ASP:Textbox>
<ew:CalendarPopup runat="server" id="calDemo1" Nullable="True" SelectedDate='<%# Container.DataItem("erecti
</EditItemTemplate>
</asp:TemplateColumn>
Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
'get a reference to the title and publication date text boxes
Dim objPubDateCtrl, objTitleCtrl As TextBox
objPubDateCtrl = CType(objArgs.Item.FindCon
'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE cell_series SET " _
& "start_Date='" & objPubDateCtrl.Text & "' " _
& "WHERE Block_no='" & MyDataGrid.DataKeys(objArg
ExecuteSQLStatement(strSQL
'set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataGrid.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub