I have a form with a Data Grid View for personnel assigned to a project. I currently have a singular drop down that allows you to add people to the project which pulls all names of people based on a main roster. I figured out how to do this by editing the columns properties and using DisplayMember however, it only allows for one column selection and not an expression.
We have only a couple of people with the same last name however, it's caused headaches in the past. I want the drop down to pull in last name, first name. I've read a few things like using an expression property but haven't be able to find that. It seems a bit different than other posts I've read mainly because Visual Studio created Binding Sources and Table Adapters and I can't seem to edit this individual control column. Everything I've read shows how to do this by programatically creating a table but not by editing an existing one. I have an SQL view set up that solves this but am unable to set the control to a view or another query. So what's the best way to edit the adapter without creating the column on the actual table?
Thanks for any help. Don
Followed this but wasn't able to get it to work with the DataGridView and adpaters.
CodePudding user response:
This hasn't really got anything to do with the grid or the table adapter, so that would explain why you can't find it. Expression
is a property of a DataColumn
, i.e. a column in a DataTable
, so that's where you need to make the change. If you have a typed DataSet
then you make changes to it in the DataSet
designer.
Open your DataSet
in the designer. Right-click the appropriate table and select Add -> Column. Enter the name of the column in the table, then open the Properties window and set the Expression
property. I just tested with a Person
table with GivenName
and FamilyName
columns, adding a FullName
column and setting the Expression
to GivenName ' ' FamilyName
. Once you save that, you can see in the Data Sources window that your table now has the extra column. If you drag that table onto a form, it will create a DataGridView
with a corresponding column. If you already have a DataGridView
then you can add a new column yourself and set its DataPropertyName
so it binds to your new expression column.