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.
Related
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.
I have a DataGridView that shows summary data. It is bound to a BindingSource which is bound to a DataTable. Depending on the summary type selected by the user, the program may split the data into multiple columns. In my code, I update the existing column and add columns as needed. The new column, as expected, gets added to the end of the list and I use SetOrdinal to reposition it. The DataGridView, however never moves the column unless I blank and rebind the DataSource to the BindingSource as follows:
I add columns with the following code:
this.Columns.Add(newcolumn);
this.Columns[columnname].SetOrdinal(index++);
And I refresh the source as follows:
teamstats.UpdateColumn(teamstats.Columns[columnClicked].ColumnName);
bsTBAstats.DataSource = null;
bsTBAstats.DataSource = teamstats;
This is problematic, as after I refresh the DataGridView, I have to reapply my formats (cell formts and event handlers) for each of the columns which slows down the process significantly.
Is there a more efficient way to have the DataTable refresh the column order? If the user can reorder the columns manually, why can't I do it programmatically?
The best way I've found to accomplish what I asked above is to manually resort the columns in the DataGridView to match the order of the columns in the DataTable. The following codes shows the process I used:
foreach (DataColumn c in teamstats.Columns)
dgvTBAstats.Columns[c.ColumnName].DisplayIndex = teamstats.Columns.IndexOf(c);
Seems as though the binding process should automate this, but this works. Unfortunately, if the user has manually reordered the columns, that order is lost since the opposite is true and the DataTable doesn't get reordered to match the DataGridView.
I can't seem to find a better way to bind the column order.
Is there any way I can specify that some columns in a DataTable will not be automatically bound to a DataGridView when I set the DataSource property of the DataGridView to the DataTable.
For example, if I have a DataTable with columns as "Id, Name" then can I specify that Id column will not be shown in DataGridView? I know I can set some Visible property in DataGridView after it is bound, but can I specify in the DataTable/DataColumn itself?
It's best to setup the columns and their bindings.
How to: Add and Remove Columns in the Windows Forms DataGridView Control Using the Designer
Otherwise, you can remove the unwanted columns:
How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer
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 a Windows Forms application (PC installable) in C#. I've created a DataGridView with the form designer, and am binding a DataTable to it at run-time. The DataTable is populated by a SQL call.
The DataGridView columns have been given specific HeaderText values at design-time that I want to keep. For example, "Confirmation Number" is one of my column headings. In addition, the Name property of each column has been set to match the value returned by the SQL call. So, for example, my "Confirmation Number" column has a Name property of "vchReturnReceipt" because that is the column name returned by SQL.
The data is coming back from SQL, it is getting bound to the DataViewGrid, and it is displaying, BUT the grid is creating new columns for all the returned data rather than using the pre-defined columns (the pre-defined columns are still there, and new columns are added to the right of them). For example, a new column called "vchReturnReceipt" is created, holding the value that I want to show up in the "Confirmation Number" column.
How do I display data in the existing columns, and prevent the new columns from showing up?
Before binding data do two things:
define for predefined columns a .DataPropertyName property with Database columns name
(in Designer open "Edit Columns" from Datagridview optons)
then set .AutoGenerateColumns = False
After this binding data will based on .DatPropertyName columns names