Masking a password field within a dataset [duplicate] - c#

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

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.

Use existing column names

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

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.

Force Bound DataGridView to Generate Columns and Rows

I am programmatically creating a DataGridView object and then binding it to a DataTable using the grid's DataSource property.
After creating the grid, I am adding it to a WinForms's control collection, ie:
Form1.Controls.Add(grid);
I want to apply formatting to the grid (i.e. change column header captions etc.) before passing it to the Forms.Controls collection. The problem is that the DataGridView's Rows and Columns are not created until it is added to the form.
Is there a way to force the DataGridView to create the rows and columns?
I could probably set DataGridView.Visible = false, add the grid to the form, format the grid, and then set DataGridView.Visible = true...HOWEVER, I really need to do all my formatting BEFORE I add it to the form's control collection. Any ideas?
The quick answer is no, the DataGridView has to exist in the form's control collection.
Your note about setting the control to false, then formatting it, then setting it to visible is probably the "cleanest" hack available to you.

Bind Data to DataGrid with existing columns in Windows Forms

I am trying to bind Data to a DataGridView, where the DataGrid already have columns, and if i have columns like TextBox columns it works perfectly, but the problem is if i have columns like ComboBox.
I am trying to do this in Windows Forms. I am just trying to understand the problem when i load the data to a DataTable from the Database. For example, i am loading the Data from the Database to a Datatable and then i set it as DataSource of the DataGridView, but how i can put the data in customized columns where columns are of ComboBox type?
Try this link.
Binding a Combobox control to a separate source within a DataGrid
Binding data with comboBox in DataGridview
DataGridViewComboBoxColumn Class
combobox column in datagridview after binding the table
Regards

Categories