question

ArkadyBron-8521 avatar image
0 Votes"
ArkadyBron-8521 asked ArkadyBron-8521 answered

Datagridview selectedvalue where there are duplicate display column entries.

Is there anyway that anyone knows, that you can coerce a datagridview combobox column to use the combox's selected value, rather than it's default behaviour.

For reasons I cannot fathom, the DGV combobox column, instead of using the selectedvalue returned by the edittingcontrol, uses the formatted text value to query the datasource and then uses the value of the first item it comes to.

This is OK if all of the text display member items are unique, but falls down rather badly when they aren't.

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Hello,

Both of the following use SQL-Server yet the code of interest will work with any database as we are detached from the actual database.

Don't get hung-up on what database is being used and in regards to the C# code sample using Entity Framework, don't get hung-up on this as the main focus is using the BindingList for performing updates rather than using DataGridView events other the DataGridViewComboBox as in that code sample I use projections which means updates must be handled slightly different than the other fields that are not DataGridViewComboBox columns.

Also note both code samples were done for teaching.

  • VB.NET see the following code sample using conventional data provider methods and SQL-Server.

  • C# code sample using Entity Framework with BindingList/BindingSource to get at changed data, see Customer. See this form where the DataGridView changes are updated immediately.

C# screenshot

67685-dgv1.png

Notes

  • One should always work from the DataGridView via a DataSource rather than from DataGridView cells directly.

  • When formatting is needed consider using CellFormatting event and know with many rows and columns you may expect the DataGridView to be slower than without this event which is why there is also virtual mode.





dgv1.png (61.7 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Maybe I didn't explain the problem very clearly, but I don't see how this helps.
The problem that I am trying to find a solution or workaround for, is a function of the DataGridView's own behavior. It is independant of the bindingsource, since it occurs before the data is written back to the cell.

0 Votes 0 ·

I was showing how to use a DataGridViewComboBox column knowing full well that

  • You didn't supply any code to go by so I provided a base set of code.

  • You didn't indicate which language so I provided both C# and VB.NET

  • You mentioned but falls down rather badly when they aren't, there should never but duplicates for a DataGridViewComboBox column. You need to make sure of this

  • Most coders don't know how to work with this column type properly which leads to various issues.

  • Experienced coders that work with a DataGridView should be using a BindingSource while novice coders don't see the many benefits of using them.

  • If you are not using a DataSource for the DataGridView that is indeed a bad idea.

So with the above you need then to show your code, provide a screenshot and more details as mentioned above provides how to properly use a DataGridView column.

Lastly, what do you mean by default behavior as I or others don't know what you mean.




0 Votes 0 ·
ArkadyBron-8521 avatar image
0 Votes"
ArkadyBron-8521 answered karenpayneoregon commented

I didn't provide any code, because this is a more generalised question about DGV behaviour and I don't have access to MS source code. Nothing in my code is going to illustrate the problem. I didn't mention my coding language either for the same reason. Although it is C#.

I mean by 'Default behaviour' the behaviour of the DGV when none of it's functions have been overriden.

Duplicates arise in this scenario in the following way:
The bindingsource contains a table which is an equipment manifest. Two or more equipment items that exist in different physical locations can have the same SystemName but with a different Id's.

In this scenario the display member of the DGV combobox is the SystemName and the Value Member is the Id.

What should happen is that when you select an Item from the list, the SelectedValue is stored in the Cell and therefore the binding sources underlying table.

What actually happens, is that Columns editting control (a DGV ComboBox) sends both it's SelectedValue and it's EditingControlFormattedValue to the cell. The DGV Cell then discards the SelectedValue and does a lookup using the EditingControlFormattedValue (the text of the SystemName) and returns the ID of the first item found.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Duplicates arise in this scenario in the following way: The bindingsource contains a table which is an equipment manifest. Two or more equipment items that exist in different physical locations can have the same SystemName but with a different Id's.

Usually this would be handled by having different categories for the equipment.

What actually happens, is that Columns editting control

I show how to handle this in the C# code sample.


0 Votes 0 ·
ArkadyBron-8521 avatar image
0 Votes"
ArkadyBron-8521 answered

First of all Karen, I would like to thank you most sincerely for taking the time to try and help. Your efforts really are keenly appreciated. I am only sorry that I was not able to communicate my problem, in a more easily apprehended manner.

As it happens, I have now managed to find a working solution to the problem, by overriding the DataGridViewCell's -- ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) -- function.

Then, by grabbing the EdittingControls SelectedValue and returning that instead from this function to the base, the problem is solved.

There is a little more to it than that, but that's essentially the key to it.

Again, thank you for your time and your kindness.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.