Datagridview selectedvalue where there are duplicate display column entries.

Arkady Bron 1 Reputation point
2021-02-12T16:10:43.56+00:00

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
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2021-02-13T12:37:29.03+00:00

    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.

  2. Arkady Bron 1 Reputation point
    2021-02-13T15:01:51.203+00:00

    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.


  3. Arkady Bron 1 Reputation point
    2021-02-13T22:30:33.233+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.