I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?
The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.
As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.
You can pre-create your columns in the designer. If the name of the column matches the name of the property the column will end up bound to, the databinding will take care of the DGV population for you as before.
Related
I'm interested in creating a DataGridView Column that contains multiple control objects. Specifically, I'd like to build a Column that contains: a text field, a list dropdown, a few checkboxes, and a few radio buttons.
I did some research and came across this page, http://www.codemag.com/article/0707061, which gives a general idea for what I need to do but is it little bit more complex than what I'm looking for. I've not been able to find any examples of adding multiple control objects to a single column.
I'm looking for other resources that I can reference to help me with this task.
This is being done in winforms.
Looks like I'll need to build a custom DataViewGridCell for each control that I want. I can then set the column's .CellTemplate to that Cell. Can a column have multiple CellTemplates?
The short version is like you suggested. It would be to create a custom ViewCell that Inherts from DataGridViewCell contains each of the controls you want to add. Then set the template to that column.
DataGridViewColumn column = dataGridView.Columns[indexForYourColumn];
DataGridViewCell cell = new YourCustomDataGridViewCell();
column.CellTemplate = cell;
Some specific resources to use CellTemplate, CustomControls1, CustomControls2
I use a DataGridView to view and edit data in my PostgreSQL database, which works fine. I want to make it a little more user-friendly by choosing the right Control to input data. Specifically, I want to create a ComboBox in a column to let the user search and select a value fast.
To do so, I think I need the EditingControlShowing event to fill the combobox. However, the column I get is of type DataGridViewTextBoxColumn, so the corresponding e.Control is a TextBox instead of a ComboBox.
I never initialise those columns, because they come from views in the database. How can I cast/initialise the column to DataGridViewComboBoxColumn ?
This is how I populate my DataGridView:
dgView.DataSource = getView();
getView() returns a DataTable as can be gotten from NpgsqlDataAdaper.
Since you want to use something other than a DataGridViewTextBoxColumn you need to set AutoGenerateColumns to false and actually define the columns yourself. When defining them, choose a DataGridViewComboBox column for the appropriate fields.
This can be done in code, or in the designer, and based off your question I think you should be able to simply do it through the designer.
You should do it in 2 steps.
1. First of all ( as mentioned by Michael) you should set AutoGenerateColumns to false and then create the required columns using the datagrid view columns option (in properties)
2. Then you should loop through your records and assign appropriate values to appropriate columns.
In case you have hundreds of records that you are showing in grid view, than you should consider using server side paging.
I have binded DataTable to DataGridView (in WinForms) and i'd like to change how columns are formatted in GUI.
However, if i manually add columns to DataGridView and set errorsGrid.AutoGenerateColumns = false, data doesn't appear in those columns (rows are added however). So is there any way to bind DataTable and change eg. width of column in DataGridView? With AutoGenerateColumns = false bindings like BingingList are added to existing columns, but DataTable doesn't seem to work that way.
Please tell have you specified DataPropertyName attribute for each of columns you have added manually on Edit Columns form for your grid (as on screenshot below)?
Looks like you need to specify binding to table columns explicitly for each of your grid columns on form Edit Columns which is invoked by right-click menu item Edit Columns... for grid view in form editor. You need to configure data binding for each column manually, as you have specified grid's property errorsGrid.AutoGenerateColumns = false (which means columns with bidings to table fields will not be generated automatically). As grid contains empty rows - you can be sure that data is loaded successfully, and the only thing which remains to do - is to configure way how rows are displayed in the grid.
I've found the best way to handle datagridview formatting when bound data source is to apply it AFTER the binding. You would need to reference the column either by index or by the table column name.
I have a DataGridView with a few DataGridViewComboBoxColumn's where the actual value is tied to an ID but the DisplayMember is the string counterpart in a lookup table. I'm trying to make it so when I sort by that column then the sort is done based on the DisplayMember, not the ValueMember. I know this was addressed in this question but the answer was less than in depth nor did I understand it.
What I've tried thus far
Binding to the SortCompare event but discovered it isn't fired on a databound column.
Manually sorting on the ColumnHeaderMouseClick event but rows in a DataGridViewRowCollection are read-only and I can't programatically insert rows (while swapping) on a databound collection.
Creating a hidden DataGridViewTextBoxColumn where the cells are automatically set to the DisplayMember of the original column then attempting to sort that column instead. However, a databound collection cannot be sorted based on an unbounded column.
Edit: To further clarify: I'm attempting to sort the entire DataGridView based on the DisplayMember of the comboboxes, not sort the comboboxes themselves.
How can I sort a DataGridView based on the DisplayMember of a databound DataGridViewComboBoxColumn?
Your third attempt is almost right: What you need to do is create an extra column in your DataSet to store the value of the DisplayMember. Then, you create an invisible bound DataGridViewColumn, and bind it to this extra DataSet column. Then it's a bound column, and you can programmatically sort on it.
The question you've linked to is doing the same, only that solution is generating the display member text in SQL, before it's returned to the application.
You'd think this'd be simple, wouldn't you? :)
I have a column in my DataGridView that is a custom type to display values of MyObjectType. My DataTable has the column created as MyObjectType, but the DataGridView creates a DataGridViewTextBox column and populates it with MyObjectType.ToString(); which is not the behavior I want. I need to display both an image and a text value for the MyObjectType value in a single column; similar to how the name column in explorer when using details view so I can't shoehorn the data into a single column like the default behavior.
Can I either force the data to be loaded into preexisting columns; or is there a away to specify that a MyObjectTypeColumn should be created to display MyObjectType data?
Sounds like you may need to create a custom DataGridView Column.
http://www.code-magazine.com/article.aspx?quickid=0707061&page=1
http://msdn.microsoft.com/en-us/library/7fb61s43.aspx
You have to set your DisplayMember and ValueMember if you are binding, I believe. I had a similar problem when binding to a ComboBox and I was just getting String[] array as well.
Setting the AutogenerateColumns property to false allowed me to use columns with types that I defined. Am I blind though, or is that property not exposed in the designer grid.