I have a dialog with a WinForms DataGridView that shows a user list. Each row contains an AD-User combobox (DataGridViewComboboxColumn) that can grow very large (10K+ items). This DataGridView is used to link the users with their corresponding AD-Users. The content of this combobox does not change between rows.
For performance reasons, I want to change this combobox to something that scales better.
I was thinking about using a TextBox with a small selection button in it. The button then would open a modal dialog for selecting the AD-User. The cell would then contain the selected AD-User object. Alternatively this dialog could also open on a cell-doubleclick, but I think that would not be very intuitive.
Would this be the best option for replacing this combobox? If you have other/better ways of handling such a selection, please let me know.
And if so: How can I create such a custom DataGridView-Cell (TextBox + Button)?
The easiest solution is making the column readonly, then use a button column after that. Then handle
CellContentClickevent ofDataGridViewto detect button clicks.Another option is creating a custom column based on DataGridViewButtonColumn. Something like this with a bit change to shows the value of cell instead of label text.
You can also achieve it without creating any new column type by just custom painting the cell, something like this.
Example
My choice is deriving from
DataGridViewbutton column for better user experience, for example highlighting the button on mouse move. To do so, I'll change this answer which shows how to render a label and a button in a single cell. Here I change it a bit to show cell values as a label text:Handle click on button
To handle the click on the button, handle
CellContentClicklike a normalDataGridViewColumnButtonand check ife.RowIndex > -1ande.ColumnIndex == your desired column index: