TCmullet

TCmullet
- Name: [not set]
- Favorite Languages: [not set]
- Website: [not set]
- Location: [not set]
- About Me: [not set]
-
C# Tutorial - Binding a DataGridView to a Database
09/09/2010 - 21:53
Another quirk I discovered is when my grid allows new records to be added. In that case, my CellFormatting event code (the e.value=) has to be preceded by
or when the blank row paints, it bombs with a "Object reference not set to an instance of an object" message.if (e.Value == null)
return; -
C# Tutorial - Binding a DataGridView to a Database
09/09/2010 - 21:33
Works great! Thanks! But had a wrinkle, solved in a couple ways. When this executes against columns that have a date, the Foxpro date gets converted to a C# 'datetime' with the result that it shows a '12:00 am' (or the like) as part of the date. So I must exclude my date fields. 1st way was this:
Then after much further research, I found I could detect the data type and make the IF simpler:if (dgridCust.Columns[e.ColumnIndex].Name.ToLower() == "CustDOB".ToLower() ||
dgridCust.Columns[e.ColumnIndex].Name.ToLower() == "CDateAdded".ToLower())
return; // do not trim dates (it causes time to appear
e.Value = e.Value.ToString().Trim(); // get rid of foxpro's trailing blanks
Seems to do the job fine, although I'm still not sure why my VFP date-only field got converted to a date-time. Intuitively I suspect it has something to do with c# having all dates as date-times, but then I wonder why the times didn't show up before I added a CellFormatting event.if (dgridCust.Columns[e.ColumnIndex].ValueType == typeof(DateTime))
return; -
C# Tutorial - Binding a DataGridView to a Database
09/09/2010 - 16:01
How can I add logic to modify the contents of a cell as the "fill" is occurring? I can't change my query to accomplish this. Shouldn't there be some event I can put some code in? (I've converted my database from Access to Foxpro. Access stores all text fields trimmed, but Foxpro has fixed length fields. I want to trim the blanks off of the text fields so that when the user edits the datagridview, they won't find all those trailing blanks out there.) I've searched for one, but can't find one I can make sense of.
-
C# Tutorial - Binding a DataGridView to a Database
05/22/2010 - 16:07
I'm very happy and grateful for this method that programmatically binds a datagridview to a data source. Can anyone tell me what I'd have to do (in terms of the original tutorial) to replace one of the columns, which presently defaults to a DataGridViewTextBoxColumn, with the same data but as a DataGridViewComboBoxColumn? The combobox will be a small fixed set of text values. I am using an Access 2000 database, and my query is "SELECT * FROM Products". The Access text field is named "UnitsType". Two of the valid values for UnitsType are "drop" and "vcap".
Recent Comments