Adding functionality to DateTimePicker on DataGridView Control
-
Saturday, May 05, 2012 6:26 AM
hi i want to add date time picker in datagridview vs2010 form C#? i visited on this link
it works fine but doesnt allow to create rows at run time and give this error
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
need help
thanks in advance
Ali Muhammad
All Replies
-
Sunday, May 06, 2012 4:21 AM
Hi,
How are you setting datasource to datagridview? If the datasource is through datatable, you should be updating datatable.
check from here.
Hope this helps you.
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Thursday, May 10, 2012 4:58 AM
-
Sunday, May 06, 2012 7:04 AM
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Hi:)
This problem usually happens on it that when you do a binding to a DataGridView,and all the data contents must be from DataSource instead of calling DataGridView.Rows.Add(……);
【Solution】
Please do adding or inserting directly to the DataSource instance instead of do inserting to the DataGridView itself……
【Sample:Suppose you are binding to the GridView by setting its Datasource as a DataTable,then you can do this】
DataTable.Rows.Add(……); DataGridView1.DataSource = Nothing; //Re-databind DataGridView1.DataSource = DataTable;
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Thursday, May 10, 2012 4:58 AM
-
Sunday, May 06, 2012 10:32 AM
First off this is done in VB.NET, if you cannot find a converter let me know, there are plenty out there on the web.
Here is an example which sets a DataTable as the source of a DataGridView where column one is text and column two uses the calendar column you are using. Code block one is the form while code block two is a helper language extension method. In this case the data is read from a xml file but it does not matter as in the end we are working with a DataTable and not xml.
Note button1 adds a row of data after we have populated the DataGridView with the DataTable.
Public Class Form1 ''' <summary> ''' Provides one click access to edit Calendar cell. ''' Downside is that whenever a calendar cell is entered ''' even for navigation it activates the calendar. The ''' alternate is pressing F2 for editing or similar logic. ''' Another method is setting the DataGridView to edit on ''' Enter which basically is what I am doing here. Lastly ''' there is always the default, three clicks to bring up ''' the full calendar (see CalendarEditingControl). ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> <System.Diagnostics.DebuggerStepThrough()> Public Sub DGV_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) If chkCalendarOneClick.Checked Then If DataGridView1(e.ColumnIndex, e.RowIndex).EditType IsNot Nothing Then If DataGridView1.IsCalendarCell(e) Then ' This is the default out of the box behavior SendKeys.Send("{F2}") End If End If End If End Sub ''' <summary> ''' When the Name column goes into edit mode we change the default ''' colors while editing. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing If e.Control.ToString.Contains("TextBox") Then e.CellStyle.ForeColor = Color.White e.CellStyle.BackColor = Color.Red End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim query = From Employee In XDocument.Load("Person100.xml")...<Employees> _ Select PersonName = Employee.<PersonName>.Value, Employee.<Hired>.Value Dim Table As New DataTable With {.TableName = "People"} With Table.Columns .AddRange(New DataColumn() _ { _ New DataColumn("PeopleNames", System.Type.GetType("System.String")), _ New DataColumn("HiredDate", System.Type.GetType("System.DateTime")) _ } _ ) End With For Each Employee In query Table.Rows.Add(New Object() {Employee.PersonName, Employee.Hired}) Next Dim PeopleNames As New DataGridViewTextBoxColumn PeopleNames.Name = "PeopleNames" Dim HiredDate As New CalendarColumn HiredDate.Name = "HiredDate" With DataGridView1 .AutoGenerateColumns = False .AllowUserToAddRows = False .AllowUserToDeleteRows = False .Columns.AddRange(New DataGridViewColumn() {PeopleNames, HiredDate}) .DataSource = Table .RowHeadersVisible = False End With PeopleNames.DataPropertyName = "PeopleNames" PeopleNames.HeaderText = "Names" HiredDate.DataPropertyName = "HiredDate" HiredDate.HeaderText = "Hired" AddHandler DataGridView1.CellEnter, AddressOf DGV_CellEnter End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CType(DataGridView1.DataSource, DataTable).Rows.Add("Kevin", Now) End Sub End ClassCode module only
<System.Runtime.CompilerServices.Extension()> _ <System.Diagnostics.DebuggerStepThrough()> _ Function IsCalendarCell(ByVal GridView As DataGridView, ByVal e As DataGridViewCellEventArgs) As Boolean Return TypeOf GridView.Columns(e.ColumnIndex) Is CalendarColumn AndAlso Not e.RowIndex = -1 End Function
KSG
- Edited by KevininstructorMicrosoft Community Contributor Sunday, May 06, 2012 10:33 AM
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Thursday, May 10, 2012 4:59 AM
-
Monday, May 07, 2012 5:26 AMModerator
Hi Ali Muhammad,
How is it going with our friends’ suggestions?
Would you please let us know the result?
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us



