Use existing column names - c#

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

Related

Bound DataGridView with combo box [duplicate]

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.

Working with LINQ to SQL and DataGridView

I have an application in which I'm using LINQ-to-SQL as a data source. I'm currently working on some extended functionality for retrieving records from the database, and I want to display these records properly in a DataGridView component. I already have the records displaying, but there are two issues I have with how they're displaying. Specifically:
The column names are the same as in the database. I would like to abstract this away from the user, as all they are doing is selecting the record they want to load into the application. Is there a way using LINQ-to-SQL to alias the column names when you display them in a DataGridView?
There are a lot of unnecessary columns displaying in each row in the DataGridView. Specifically, I am selecting a record from an "Identifier" table, which has relationships to various other tables, and these tables are appearing as objects in the record view. Is there a way to omit certain columns from the DataGridView?
I'd appreciate any help someone could give me.
You can set the AutoGenerateColumns property value to false, which will prevent generating columns from datasource. Then add your desired columns to DataGridView, then set DataPropertyName properties of that columns to your datasource's columns names (for example, if its DataTable object - set DataTable column names[which you want to show]) for example:
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add("Col1", "Column1");
dataGridView1.Columns.Add("Col2", "Column2");
dataGridView1.Columns.Add("Col3", "Column2");
. . .
dataGridView1.Columns["Col1"].DataPropertyName = "Col1NameFromSource";
dataGridView1.Columns["Col2"].DataPropertyName = "Col2NameFromSource";
dataGridView1.Columns["Col3"].DataPropertyName = "Col3NameFromSource";
. . .
Then just bind your source to DataGridview's DataSource
DataTable table = /*Your "LINQ-to-SQL" query;*/
dataGridView1.DataSource = table;
Assuming, that your table contains Col1NameFromSource, Col2NameFromSource, Col3NameFromSource.
Use a datagrid and datatemplate if you are on WPF
https://wpftutorial.net/DataGrid.html

Masking a password field within a dataset [duplicate]

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

Define the column control of a database-driven DataGridView

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.

How to modify data grid view columns with data table bindings

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.

Categories