We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


DataGridView binding to custom design time columns

Medium Priority
Last Modified: 2012-05-06
Hi All,

I have a simple table that I would like to bind a datagridview to.  The table comprises of a pk, a date field, amount field and an fk.  If I bind the view to a binding source bound to a table (as per the dozens of examples up here) all works fine.  However I wanted the date field to work as a date picker, so I used the example found on http://msdn.microsoft.com/en-us/7tas5c80.aspx it also works well.  However, combining the two is seeming problematic.  I set the date column to type "calendar" at design time and the amount to textbox.  I set the view to autogeneratecolumns to false, but nothing gets populated when it runs and should I click on the date column cells I get a null ref exception.  

Anyone with any ideas on how to get this combination to work?

Watch Question

If you followed the Microsoft tutorial exactly, then the following should help.  

To replicate your situation I did the following:
  • Created a database named TestDB with a table named DateAmount that has ID, Date, and Amount columns
  • Created a Windows Forms project and dropped on a DataGridView control
  • Under Choose Data Source I selected Add Project Data Source and selected Database
  • Clicked on New Connection and connected to my TestDB
  • Selected the Date and Amount columns to create the DataSet
  • Created a new class then Copy/Pasted in the Microsoft tutorial code
  • Selected EditColumns and changed the column type of the Date column to my new CalendarColumn class
So given all of those steps, I believe we are in the same place.  To fix your problems do the following:

  1. Make sure your table contains data (this should be a given, but we'll eliminate the obvious problem first)
  2. Make sure there is a call to your table adapter in the form's Load event.  This will ensure that your DataGridView is loaded with the initial data from your table.  My call looks like: this.dateAmountTableAdapter.Fill(this.testDBDataSet.DateAmount);
  3. Fix the crash by changing the InitializeEditingControl method in the CalanderColumn class.  You will be adding a check to this.Value to ensure that it is a DateTime object before assigning.
Following those steps should give you a functioning DataGridView object.  Hope this helps, and good luck!

//Form Load for TableAdapter fill
private void Form1_Load(object sender, EventArgs e)
		// TODO: This line of code loads data into the 'testDBDataSet.DateAmount' table. You can move, or remove it, as needed.
//Change this method to fix the null ref exception
public override void InitializeEditingControl(int rowIndex, object
	 initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
	// Set the value of the editing control to the current cell value.
	base.InitializeEditingControl(rowIndex, initialFormattedValue,
	CalendarEditingControl ctl =
		 DataGridView.EditingControl as CalendarEditingControl;
        //This line was added to make sure that the Value is a valid DateTime before assigning.
	if (this.Value is DateTime)
		ctl.Value = (DateTime)this.Value;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks for your help on this jzierse,

I have done as you suggest and things are much improved!  My remaining issue is in which event would you run the adapter.update function.  I have it in the CellValueChanged event and it errors out as it is firing on the update of one of the two cells.  I need some kind of row update event.  Hmmmm...

Thanks again!

Before calling update, check that the RowIndex and ColumnIndex being passed into the event are greater than or equal to zero.  This method gets called when the table is first initialized and those values can be invalid.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
	if ((e.ColumnIndex >= 0) && (e.RowIndex >= 0))
//Update code here

Open in new window

Also make sure that if any of your database columns are set to not allow a column to be null, you will need to make sure that none of the DataRow's values are null.


You know, I simply could not get this to work.  I continued to have issues in the calendar control, and then I could not validate the row before row events would fire and insert nulls into the table causing further errors.

It seems that examples of the combination of a datagridview, custom controls and custom commands is a rare occurence, so I've thrown in the towel and gone with the ol' tried and true listview, insert forms and add/del buttons...

Moderator, please refund the points.

Well, since I did answer your original question, and put in a bit of effort doing so, I would appreciate a few points.  Thanks.


Yes, you did help on the original post - apologies for the oversight.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.