Search code examples
sqlvb.netdatagridviewdatagridviewcombobox

Is it possible to insert unbound column into a bound datagridview?


I have a datagridview, bound to data and running swell. The data in two columns are integers, but they have a meaning that I would like the form user to see instead.

Column 1       Column 2       Column 3       Column 4
_____________________________________________________
Bob            1              2               Yes
Mary           1              3               No
Tod            2              2               No
Beth           3              3               Yes

Where 1, 2, and 3 are something like job skills. So I want to hide column 2 and 3 and interpret back and forth from SQL.

Column 1       Column 2       Column 3       Column 2a        Column 3a        Column 4
_______________________________________________________________________________________
Bob            1              2              Accounting      Admin            Yes
Mary           1              3              Accounting      Manager          No
Tod            2              2              Architect       Admin            No
Beth           3              3              IT              Manager          Yes

Only imagine Columns 2 & 3 are hidden and maybe 2a & 3a are comboboxes that have code behind them that convert the strings to the integers.

The project is too large to fix it to use strings in the first place. I'm open to other suggestions like make Columns 2 & 3 comboboxes that are both databound and function based on another SQL command. I cannot see how that would work though.


Solution

  • From Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET:

    There are two primary ways to populate the contents of unbound columns: handling the RowsAdded event or handling the CellFormatting event. The former is a good place to set the cell’s value to make it available for programmatic retrieval later. The latter is a good place to provide a value that will be used for display purposes only and won’t be stored as part of the data retained by the grid cells collection. The CellFormatting event can also be used to transform values as they are presented in a cell to something different than the value that is actually stored in the data that sits behind the grid.