Hi Leolander, Edit date in a Datagrid?? Are you try to retrieve a DateTime type data from Database and let it editable from user? If so, the most commonly use approach would be put an icon next to the TextBox in the EditItemTemplate, and then PopUp a Calendar
control for user selection. Finally you can sure (convert) insert this modified data into the Database. Regards,
I am trying to do this same kinda thing in a data grid but I keep getting this error Compiler Error Message: BC30451: Name 'insertdate' is not declared. Source Error: Line 71: MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex) Line 72: BindGrid() Line 73: insertdate.Attributes.Add("onclick",
" Insertdate('edit_lastcontact')") Line 74: insertdate2.Attributes.Add("onclick", " Insertdate('edit_nextcontact')") Line 75: End Sub but it clearly is declared
Read these 2 articles: http://aspnet.4guysfromrolla.com/articles/040502-1.aspx - this is part 1 of a serie of 15 articles written by Scott Mitchell (An Extensive Examination of the DataGrid Web Control) and http://aspalliance.com/Colt/Articles/Article4.aspx
(Popup calendar)
I went through the links you provided and skimmed over them the popup calendar is were I got the idea from orginally the only page in the first series of articles that I found related a little bit was http://aspnet.4guysfromrolla.com/articles/090902-1.aspx
the on focus area my problem is that the datagrid dynamically labels the textboxes so I don't really know how to target a javascript on it
ok I have been racking my brain over it all night looking at those links and trying to convert that to what I need the closest I can come is Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:
Line 77: BindGrid() Line 78: Dim nextcontacttb as Textbox = MyDataGrid.Items(e.Item.ItemIndex).Cells(2).FindControl("lastcontact") Line 79: insertdate.Attributes.Add("onclick", " Insertdate('"& MyDataGrid.ClientID &"')")
<script runat="server" language="VB">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
ViewState("SortExpr") = "DateAdded ASC"
BindData(ViewState("SortExpr"))
End If
End Sub
Sub BindData(sortExpr as String)
'1. Create a connection
Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn as New SqlConnection(strConnString)
'2. Create a command object for the query
Dim strSQL as String
strSQL = "SELECT CommentID, Name, Comment, DateAdded " & _
"FROM Comments ORDER BY " & sortExpr
Dim objCmd as New SqlCommand(strSQL, objConn)
'3. Create a DataAdapter and Fill the DataSet
Dim objDA as New SqlDataAdapter()
objDA.SelectCommand = objCmd
objConn.Open()
Dim objDS as DataSet = New DataSet()
objDA.Fill(objDS, "titles")
objConn.Close()
'Finally, specify the DataSource and call DataBind()
dgComments.DataSource = objDS
dgComments.DataBind()
objConn.Close() 'Close the connection
ShowPageInformation()
End Sub
Sub ShowPageInformation()
'This sub displays paging information in the appropriate label
lblPagingInfo.Text = "Displaying Page " & _
(dgComments.CurrentPageIndex+1).ToString() & " of " & _
dgComments.PageCount
End Sub
Sub dgComments_Paging(sender As Object, e As DataGridPageChangedEventArgs)
'Turn off editing
dgComments.EditItemIndex = -1
dgComments.CurrentPageIndex = e.NewPageIndex
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_Sorting(sender as Object, e as DataGridSortCommandEventArgs)
'Set the sortExpr ViewState variable accordingly
ViewState("SortExpr") = e.SortExpression
'Reset the data to show the FIRST page of data
dgComments.CurrentPageIndex = 0
'Turn off editing
dgComments.EditItemIndex = -1
'Bind the data!
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_EditRow(sender As Object, e As DataGridCommandEventArgs)
dgComments.EditItemIndex = e.Item.ItemIndex
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_UpdateRow(sender As Object, e As DataGridCommandEventArgs)
'Make sure the Page is Valid
If Not Page.Isvalid Then Exit Sub
'Get information from columns...
Dim nameTextBox as TextBox = e.Item.Cells(1).FindControl("txtName")
Dim commentTextBox as TextBox = e.Item.Cells(2).FindControl("txtComment")
Dim dateAdded as Calendar = e.Item.Cells(3).FindControl("calDate")
Dim iCommentID as Integer = dgComments.DataKeys(e.Item.ItemIndex)
'Update the database...
Dim strSQL as String
strSQL = "UPDATE Comments SET Name = @NameParam, DateAdded = @DateAddedParam, " & _
"Comment = @CommentParam WHERE CommentID = @CommentIDParam"
Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn as New SqlConnection(strConnString)
Dim objCmd as New SqlCommand(strSQL, objConn)
Dim nameParam as New SqlParameter("@NameParam", SqlDbType.VarChar, 50)
Dim commentParam as New SqlParameter("@CommentParam", SqlDbType.VarChar, 255)
Dim dateAddedParam as New SqlParameter("@DateAddedParam", SqlDbType.DateTime)
Dim commentIDParam as New SqlParameter("@CommentIDParam", SqlDbType.Int, 4)
nameParam.Value = nameTextBox.Text
objCmd.Parameters.Add(nameParam)
commentParam.Value = commentTextBox.Text
objCmd.Parameters.Add(commentParam)
dateAddedParam.Value = dateAdded.SelectedDate
objCmd.Parameters.Add(dateAddedParam)
commentIDParam.Value = iCommentID
objCmd.Parameters.Add(commentIDParam)
'Issue the SQL command
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
dgComments.EditItemIndex = -1
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_CancelRow(sender As Object, e As DataGridCommandEventArgs)
dgComments.EditItemIndex = -1
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_DeleteRow(sender As Object, e As DataGridCommandEventArgs)
'Turn off editing
dgComments.EditItemIndex = -1
Dim iCommentID as Integer
iCommentID = dgComments.DataKeys(e.Item.ItemIndex)
'Update the database...
Dim strSQL as String
strSQL = "DELETE FROM Comments WHERE CommentID = @CommentIDParam"
Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn as New SqlConnection(strConnString)
Dim objCmd as New SqlCommand(strSQL, objConn)
Dim commentIDParam as New SqlParameter("@CommentIDParam", SqlDbType.Int, 4)
commentIDParam.Value = iCommentID
objCmd.Parameters.Add(commentIDParam)
'Issue the SQL command
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
BindData(ViewState("SortExpr"))
End Sub
Sub dgComments_RowDataBound(sender as Object, e As DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header AND _
e.Item.ItemType <> ListItemType.Footer then
Dim deleteButton as LinkButton = e.Item.Cells(1).Controls(0)
'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete the following comment?');"
End If
End Sub
</script>
<form runat="server">
<asp:DataGrid runat="server" id="dgComments"
Font-Name="Verdana" Font-Size="9pt" CellPadding="5"
AlternatingItemStyle-BackColor="#dddddd"
AutoGenerateColumns="False" Width="75%"
PageSize="10" AllowPaging="True"
OnPageIndexChanged="dgComments_Paging"
AllowSorting="True" OnSortCommand="dgComments_Sorting"
OnEditCommand="dgComments_EditRow"
OnUpdateCommand="dgComments_UpdateRow"
OnCancelCommand="dgComments_CancelRow"
DataKeyField="CommentID"
OnDeleteCommand="dgComments_DeleteRow"
OnItemDataBound="dgComments_RowDataBound">
<HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="13pt"
Font-Bold="True" HorizontalAlign="Center" />
<PagerStyle BackColor="Navy" ForeColor="White" Font-Size="8pt"
Font-Bold="True" HorizontalAlign="Right"
NextPageText="Next >" PrevPageText="< Prev" />
<asp:EditCommandColumn EditText="Edit" UpdateText="Update"
CancelText="Cancel" ButtonType="LinkButton" />
<asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
CommandName="Delete" />
<asp:TextBox runat="server" id="txtName" Columns="15"
MaxLength="50" Font-Name="Verdana" Font-Size="9pt"
Text='' />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtName" Display="Dynamic"
ErrorMessage="
You must provide a Name." />
<asp:TextBox runat="server" id="txtComment" Width="95%"
MaxLength="255" Font-Name="Verdana" Font-Size="9pt"
TextMode="MultiLine" Rows="5"
Text='' />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtComment" Display="Dynamic"
ErrorMessage="
You must provide a Comment." />
<asp:TemplateColumn HeaderText="Date Added" SortExpression="DateAdded"
ItemStyle-HorizontalAlign="Center">
<asp:Calendar id="calDate" runat="server"
SelectedDate=''
VisibleDate='' />
<asp:label id="lblPagingInfo" runat="server" Font-Name="Verdana"
Font-Size="9pt" Font-Italic="True" Width="75%" HorizontalAlign="Right" />
</form>
leolander
Member
8 Points
27 Posts
editing of dates
Nov 21, 2002 06:58 AM|LINK
Colt
All-Star
15569 Points
1986 Posts
ASPInsiders
MVP
Re: editing of dates
Nov 21, 2002 07:41 AM|LINK
TorontoVB
Participant
765 Points
153 Posts
Re: editing of dates
Aug 28, 2003 08:48 PM|LINK
jorgecapela
Member
25 Points
5 Posts
Re: editing of dates
Aug 28, 2003 10:27 PM|LINK
TorontoVB
Participant
765 Points
153 Posts
Re: editing of dates
Aug 29, 2003 02:57 AM|LINK
TorontoVB
Participant
765 Points
153 Posts
Re: editing of dates
Aug 29, 2003 04:05 AM|LINK
jorgecapela
Member
25 Points
5 Posts
Re: editing of dates
Aug 29, 2003 09:03 AM|LINK
<script runat="server" language="VB"> Sub Page_Load(sender as Object, e as EventArgs) If Not Page.IsPostBack then ViewState("SortExpr") = "DateAdded ASC" BindData(ViewState("SortExpr")) End If End Sub Sub BindData(sortExpr as String) '1. Create a connection Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs" Dim objConn as New SqlConnection(strConnString) '2. Create a command object for the query Dim strSQL as String strSQL = "SELECT CommentID, Name, Comment, DateAdded " & _ "FROM Comments ORDER BY " & sortExpr Dim objCmd as New SqlCommand(strSQL, objConn) '3. Create a DataAdapter and Fill the DataSet Dim objDA as New SqlDataAdapter() objDA.SelectCommand = objCmd objConn.Open() Dim objDS as DataSet = New DataSet() objDA.Fill(objDS, "titles") objConn.Close() 'Finally, specify the DataSource and call DataBind() dgComments.DataSource = objDS dgComments.DataBind() objConn.Close() 'Close the connection ShowPageInformation() End Sub Sub ShowPageInformation() 'This sub displays paging information in the appropriate label lblPagingInfo.Text = "Displaying Page " & _ (dgComments.CurrentPageIndex+1).ToString() & " of " & _ dgComments.PageCount End Sub Sub dgComments_Paging(sender As Object, e As DataGridPageChangedEventArgs) 'Turn off editing dgComments.EditItemIndex = -1 dgComments.CurrentPageIndex = e.NewPageIndex BindData(ViewState("SortExpr")) End Sub Sub dgComments_Sorting(sender as Object, e as DataGridSortCommandEventArgs) 'Set the sortExpr ViewState variable accordingly ViewState("SortExpr") = e.SortExpression 'Reset the data to show the FIRST page of data dgComments.CurrentPageIndex = 0 'Turn off editing dgComments.EditItemIndex = -1 'Bind the data! BindData(ViewState("SortExpr")) End Sub Sub dgComments_EditRow(sender As Object, e As DataGridCommandEventArgs) dgComments.EditItemIndex = e.Item.ItemIndex BindData(ViewState("SortExpr")) End Sub Sub dgComments_UpdateRow(sender As Object, e As DataGridCommandEventArgs) 'Make sure the Page is Valid If Not Page.Isvalid Then Exit Sub 'Get information from columns... Dim nameTextBox as TextBox = e.Item.Cells(1).FindControl("txtName") Dim commentTextBox as TextBox = e.Item.Cells(2).FindControl("txtComment") Dim dateAdded as Calendar = e.Item.Cells(3).FindControl("calDate") Dim iCommentID as Integer = dgComments.DataKeys(e.Item.ItemIndex) 'Update the database... Dim strSQL as String strSQL = "UPDATE Comments SET Name = @NameParam, DateAdded = @DateAddedParam, " & _ "Comment = @CommentParam WHERE CommentID = @CommentIDParam" Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs" Dim objConn as New SqlConnection(strConnString) Dim objCmd as New SqlCommand(strSQL, objConn) Dim nameParam as New SqlParameter("@NameParam", SqlDbType.VarChar, 50) Dim commentParam as New SqlParameter("@CommentParam", SqlDbType.VarChar, 255) Dim dateAddedParam as New SqlParameter("@DateAddedParam", SqlDbType.DateTime) Dim commentIDParam as New SqlParameter("@CommentIDParam", SqlDbType.Int, 4) nameParam.Value = nameTextBox.Text objCmd.Parameters.Add(nameParam) commentParam.Value = commentTextBox.Text objCmd.Parameters.Add(commentParam) dateAddedParam.Value = dateAdded.SelectedDate objCmd.Parameters.Add(dateAddedParam) commentIDParam.Value = iCommentID objCmd.Parameters.Add(commentIDParam) 'Issue the SQL command objConn.Open() objCmd.ExecuteNonQuery() objConn.Close() dgComments.EditItemIndex = -1 BindData(ViewState("SortExpr")) End Sub Sub dgComments_CancelRow(sender As Object, e As DataGridCommandEventArgs) dgComments.EditItemIndex = -1 BindData(ViewState("SortExpr")) End Sub Sub dgComments_DeleteRow(sender As Object, e As DataGridCommandEventArgs) 'Turn off editing dgComments.EditItemIndex = -1 Dim iCommentID as Integer iCommentID = dgComments.DataKeys(e.Item.ItemIndex) 'Update the database... Dim strSQL as String strSQL = "DELETE FROM Comments WHERE CommentID = @CommentIDParam" Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs" Dim objConn as New SqlConnection(strConnString) Dim objCmd as New SqlCommand(strSQL, objConn) Dim commentIDParam as New SqlParameter("@CommentIDParam", SqlDbType.Int, 4) commentIDParam.Value = iCommentID objCmd.Parameters.Add(commentIDParam) 'Issue the SQL command objConn.Open() objCmd.ExecuteNonQuery() objConn.Close() BindData(ViewState("SortExpr")) End Sub Sub dgComments_RowDataBound(sender as Object, e As DataGridItemEventArgs) If e.Item.ItemType <> ListItemType.Header AND _ e.Item.ItemType <> ListItemType.Footer then Dim deleteButton as LinkButton = e.Item.Cells(1).Controls(0) 'We can now add the onclick event handler deleteButton.Attributes("onclick") = "javascript:return " & _ "confirm('Are you sure you want to delete the following comment?');" End If End Sub </script> <form runat="server"> <asp:DataGrid runat="server" id="dgComments" Font-Name="Verdana" Font-Size="9pt" CellPadding="5" AlternatingItemStyle-BackColor="#dddddd" AutoGenerateColumns="False" Width="75%" PageSize="10" AllowPaging="True" OnPageIndexChanged="dgComments_Paging" AllowSorting="True" OnSortCommand="dgComments_Sorting" OnEditCommand="dgComments_EditRow" OnUpdateCommand="dgComments_UpdateRow" OnCancelCommand="dgComments_CancelRow" DataKeyField="CommentID" OnDeleteCommand="dgComments_DeleteRow" OnItemDataBound="dgComments_RowDataBound"> <HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="13pt" Font-Bold="True" HorizontalAlign="Center" /> <PagerStyle BackColor="Navy" ForeColor="White" Font-Size="8pt" Font-Bold="True" HorizontalAlign="Right" NextPageText="Next >" PrevPageText="< Prev" /> <asp:EditCommandColumn EditText="Edit" UpdateText="Update" CancelText="Cancel" ButtonType="LinkButton" /> <asp:ButtonColumn Text="Delete" ButtonType="LinkButton" CommandName="Delete" /> <asp:TextBox runat="server" id="txtName" Columns="15" MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='' /> <asp:RequiredFieldValidator runat="server" ControlToValidate="txtName" Display="Dynamic" ErrorMessage=" You must provide a Name." /> <asp:TextBox runat="server" id="txtComment" Width="95%" MaxLength="255" Font-Name="Verdana" Font-Size="9pt" TextMode="MultiLine" Rows="5" Text='' /> <asp:RequiredFieldValidator runat="server" ControlToValidate="txtComment" Display="Dynamic" ErrorMessage=" You must provide a Comment." /> <asp:TemplateColumn HeaderText="Date Added" SortExpression="DateAdded" ItemStyle-HorizontalAlign="Center"> <asp:Calendar id="calDate" runat="server" SelectedDate='' VisibleDate='' /> <asp:label id="lblPagingInfo" runat="server" Font-Name="Verdana" Font-Size="9pt" Font-Italic="True" Width="75%" HorizontalAlign="Right" /> </form>