Hi Kira,
The Source of my DataGridView is a DataSet.
What I want is to let my DataGridView the possibility to get the type of the original data, and then change the format of the column so, if the original data is DateTime, the column will show a DateTimePicker, if the original data is True/False the column will show a CheckBox, and so on...
I am first trying to create the "Calendar column".
I don't understand what do you mean with "handling the DataBindingComplete" Event of the datagridview. Could you provide some code exapmple, please?
And again, I want to let CommandBuilder managing the changes in the database.
---------------------------------------------------------------------------------------------------------
Below a couple of details...
I am trying the codebelow, but I still have the same problem that the new "Calendar column" doesn't show the updated values (taken from "DateCreated" column).
| CalendarColumncol=newCalendarColumn(); |
| col.Name="CalendarCol"; |
| col.HeaderText="CalendarCol"; |
| this.dgv1.Columns.Add(col); |
| |
| foreach(DataGridViewRowdrinthis.dgv1.Rows) |
| { |
| if(dr.Cells["DateCreated"].Value!=DBNull.Value&&dr.Cells["DateCreated"].Value!=null) |
| { |
| dr.Cells["CalendarCol"].Value=dr.Cells["DateCreated"].Value; |
| } |
| } |
|
I checked what happens with the values in the new "CalendarCol" column after the foreach loop.
The values I copied are in the new DateTimePicker (as they should be), for example:
dgv1["CalendarCol", 0].Value.ToString()
"10/01/2004 00:00:00"
dgv1["CalendarCol", 1].Value.ToString()
"11/02/2005 00:00:00"
dgv1["CalendarCol", 2].Value.ToString()
"12/03/2006 00:00:00"
so the problem must be in some other place in the logic...
Below the code that runs after that loop (is the line that calls the form with the DataGridView)
| Main.frmGridgrid=newMain.frmGrid(tsmi.ToString(),rm,querySource); |
| grid.Show(); |
|
and then it goes back to the classes of the Calendar column (to the point highlighted in yellow)...:
| publicclassCalendarColumn:DataGridViewColumn |
| { |
| publicCalendarColumn():base(newCalendarCell()) |
| { |
| } |
| publicoverrideDataGridViewCellCellTemplate |
| { |
| get |
| { |
| returnbase.CellTemplate; |
| } |
| set |
| { |
| //EnsurethatthecellusedforthetemplateisaCalendarCell. |
| if(value!=null&&!value.GetType().IsAssignableFrom(typeof(CalendarCell))) |
| { |
| thrownewInvalidCastException("MustbeaCalendarCell"); |
| } |
| base.CellTemplate=value; |
| } |
| } |
| } |
|
Then...
| publicclassCalendarCell:DataGridViewTextBoxCell |
| { |
| publicCalendarCell():base() |
| { |
| //Usetheshortdateformat. |
| this.Style.Format="d"; |
| } |
| |
| publicoverridevoidInitializeEditingControl(introwIndex,objectinitialFormattedValue,DataGridViewCellStyledataGridViewCellStyle) |
| { |
| //Setthevalueoftheeditingcontroltothecurrentcellvalue. |
| base.InitializeEditingControl(rowIndex,initialFormattedValue,dataGridViewCellStyle); |
| CalendarEditingControlctl=DataGridView.EditingControlasCalendarEditingControl; |
| if(this.Value!=null) |
| { |
| ctl.Value=(DateTime)this.Value; |
| } |
| } |
| |
| publicoverrideTypeEditType |
| { |
| get |
| { |
| //ReturnthetypeoftheeditingcontolthatCalendarCelluses. |
| returntypeof(CalendarEditingControl); |
| } |
| } |
| |
| publicoverrideTypeValueType |
| { |
| get |
| { |
| //ReturnthetypeofthevaluethatCalendarCellcontains. |
| returntypeof(DateTime); |
| } |
| } |
| |
| publicoverrideobjectDefaultNewRowValue |
| { |
| get |
| { |
|
|
|
| returnDateTime.Now; |
| } |
|
| } |
| } |
| } |
|
And that's all. When running, the grid looks like below, and the Calendar column does not show the values, like:
Name DateCreated CalendarCol
Aldo 10/01/2004
Jim 11/02/2005
John 12/03/2006
Michal 13/04/2007
New lineā?/span> 26/03/2009
If I click on one of the (empty) cells in CalendarCol, I get a DateTimePicker with the value "26/03/2009".
I hope you can help me to solve it...
Thanks,
Aldo.
Everything is possible, impossible just takes longer