Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Convert BOUND DataGridView column to DateTimePicker column
 

Convert BOUND DataGridView column to DateTimePicker column

Hi guys,

I have a DataGridView bound to an MS Access table (.mdb).

I can populate the data from the database to my DataGridView, and I later use an OleDbCommandBuilder to saveuser changes back to the database.

In addition, Iwant my DataGridView to recognize the type of data (underlying) in each column, and change from a TextBox column to the one that matches that format, in example a colum with DateTimePicker for a "date" column.

I am trying the following MSDN example,but getting some problems:

http://msdn.microsoft.com/en-us/7tas5c80.aspx

When running the app, I can see the new column. If I click on a cell in that column, I get a DateTimePicker.

One problem is, that the cells in the new column are NOT accepting (not showing) the values I copied into them (from the column with the original data)...
It looks like I need to do some "refresh" to the cells...?

CalendarColumncol=newCalendarColumn();
col.Name="CalendarCol";
col.HeaderText="CalendarCol";
this.dgv1.Columns.Add(col);
foreach(DataGridViewRowrowinthis.dgv1.Rows)
{
if(row.Cells["CustomerBirthDate"].Value!=null)
{
row.Cells["CalendarCol"].Value=(DateTime)row.Cells["CustomerBirthDate"].Value;
}
}

The other question is,
Will I still be able to use the UpdateCommand created by theOleDbCommandBuilder to update the data in the database?

Any help will be appreciated.
Thanks in advance,
Aldo.


Everything is possible, impossible just takes longer
  • Edited byajliaks Wednesday, March 25, 2009 8:22 AMmore details
  •  
ajliaks  Tuesday, March 24, 2009 7:43 AM

Hi ajliaks,

Base on my experience. You should handle the DataBindingComplete event of the datagridview to do the copy. Otherwise the copy won't affect.

Does the CalendarCol also existed in your datasource?
If so, you can copy the data in the datasource instead of datagridview. e.g. befor you bind underline datatable to the datagridview, you can walkthrough all rows to check the CustomerBirthDate column. and set its data to the CalendarCol.

The answer to the second question is, OleDbCommandBuilder only relate to your underline datasource. Once the value is changed in datagridview, it should be commit to the datasource(a datatable for example). Then you can use that datatable to update the database. If the datatable can correctly recognize the RowState, there is no problem to use OleDbCommandBuilder.

If I misunderstood something, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, March 26, 2009 3:14 AM
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
  • Edited byajliaks Thursday, March 26, 2009 8:57 AMmore details
  •  
ajliaks  Thursday, March 26, 2009 6:36 AM

The first question is solved.

The DataGridView shows all the values (a copy from another column of the DGV).

All I did is moving the code that creates the Calendar column from the Constructor of the Form, to the Form_Load Event. Then it worked.

About the second one... I am still working on it...

Aldo.


Everything is possible, impossible just takes longer
ajliaks  Thursday, March 26, 2009 1:35 PM

You can use google to search for other answers

Custom Search

More Threads

• Sorting columns in DataGrid
• linking checkbox to the database
• BindingSource - Detail view - Is Current record dirty?
• Bug in CurrencyManager.OnPositionChanged - eats exceptions
• Simple Question, Database and windows form, why is not a two way street
• check dataset for changes
• BindingSource.PositionChanged vs DataGridView.Sort
• DataBinding - enforce read
• Finding the active column in a datagridview/textbox for FINDING
• How to get an "index" of a datagridviewcombobox?