Dear All,

i have been working on a DataGridView for quite few days. Now its comes that a particular column has to show user name in display and in internal it should return user id. DataGridView is populated from a DataSource which will only supply user id. anybody knows there is any inbilt function or property in DataGridView to implements this option ?

thanks

Warun

Dani AI

Generated

As clarified you want a combo-column that shows user names but keeps the user id as the cell value (and checked whether you meant a link — you do not). If this is a WinForms DataGridView, use a DataGridViewComboBoxColumn bound to a lookup (users) list: set the grid column’s DataPropertyName to the id field in the main data source, set the combo column’s DataSource to the users lookup, set DisplayMember to the user-name field and ValueMember to the user-id field.

Example (VB.NET, programmatic):

Dim users As DataTable = GetUsersTable() ' columns: UserID, UserName
Dim combo As New DataGridViewComboBoxColumn()
combo.HeaderText = "User"
combo.DataPropertyName = "UserID" ' main grid column that stores the id
combo.DataSource = users
combo.DisplayMember = "UserName"
combo.ValueMember = "UserID"
DataGridView1.Columns.Add(combo)

Troubleshooting notes: the combo column must have every ID that appears in the main grid’s rows (otherwise you will get a DataError when the grid tries to display a value that is not in the lookup). Either ensure the lookup contains all ids, or handle the grid’s DataError event to suppress or log the mismatch. Also bind the combo’s DataSource to a DataTable, List(Of T) or BindingSource so updates stay in sync. See the official docs for DataGridViewComboBoxColumn and handling DataError for details: DataGridViewComboBoxColumn documentation and DataGridView.DataError event.

If you are actually using ASP.NET GridView instead of WinForms, use a TemplateField with a DropDownList in the EditItemTemplate (DataTextField = name, DataValueField = id, SelectedValue bound to the UserID column) so the page shows the name but posts the id.

Recommended Answers

All 2 Replies

I didn't understand your post. You want the column to be a link that displays the user name but gives the userid to the click or command methods?

i'm looking for a combo box type column, which will display user name and there would be a internal property which will return the corresponding user id for the selected user name

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.