• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 938
  • Last Modified:

DataGridView binding to custom design time columns

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?

Thanks,
John.
0
siskinds
Asked:
siskinds
  • 4
  • 3
1 Solution
 
jzierseCommented:
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.
		this.dateAmountTableAdapter.Fill(this.testDBDataSet.DateAmount);
}
 
 
//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,
		 dataGridViewCellStyle);
	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

0
 
siskindsAuthor Commented:
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!
John.
0
 
jzierseCommented:
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

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
jzierseCommented:
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.
0
 
siskindsAuthor Commented:
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.
Thanks.
0
 
jzierseCommented:
Well, since I did answer your original question, and put in a bit of effort doing so, I would appreciate a few points.  Thanks.
0
 
siskindsAuthor Commented:
Yes, you did help on the original post - apologies for the oversight.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now