On my Windows Form I have a DataGridView component, which is bound to a BindingSource. The BindingSource is an object datasource to an EntityFramework object.
Some times the columns in my DataBridView are renewed. Sometimes all properties are added as column, but now it also removed all my columns. So i've lost all my settings.
When to columns get automatically get added?
(I'm using VS.NET 2010)
Update:
//
// Summary:
// Gets or sets a value indicating whether columns are created automatically
// when the System.Windows.Forms.DataGridView.DataSource or System.Windows.Forms.DataGridView.DataMember
// properties are set.
//
// Returns:
// true if the columns should be created automatically; otherwise, false. The
// default is true.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DefaultValue(true)]
public bool AutoGenerateColumns { get; set; }
The property did not show up in the designer, and "hide advanced properties" is not checked.
Update 2:
When I update my entity framework model, all columns are added again. I only can set the property in the constructor of the form. This is very annoying.
I actually don't know when this happens, but I tend to create all the columns manually. I create the columns in the designer and set the AutoGenerateColumns property to false in my code.
Add this code or change your DataGridView Property AutoGenerateColumns to false
DataGridView1.AutoGenerateColumns=false;
Set AutoGenerateColumns property to False but keep remember do it just before databinding.
eg:
DataGridView1.AutoGenerateColumns=false;
DataGridView1.DataSource=getData();
By default it is set to True.
Try leave first of auto generated columns and set it visibility false.
If it don't help try leave all them with Visible=false.
Sorry for bad English.
I had the same problem. I couldn't find the AutoGenerate property in my code.
For reasons I do not understand my DataGridView does not have an AutoGenerate property that I can see in my VB code.
I do not see a checkbox on the Edit Columns dialog box.
I do not see an AutoGenerate property in the grid's properties view.
I have Visual Studio Community 2017.
Here's my class properties:
Public Property BatchId As Integer
Public Property Code as String
Public Property Count As Integer
Public Property Description As String
Public Property Id As Integer
So, here's what I did:
I went to the form designer.
I right-clicked on the DataGridView.
I selected Edit Columns.
I made sure each column had the DataPropertyName field set to the class property name.
When I ran my application the DataGridView displayed only those columns in my class.
The little additional information provided by CoolBreeze was the item I was missing. I had created my columns in the designer so that I could do all the layout adjustments but then when I set the datasource property it would autogenerate the columns. I tried the suggestions about of setting the DataPropertyName in code but because I had the AutoGenerate set to false before doing that it was still putting the duplicate columns. Turns out I can set this property at design time as well and then set the AutoGenerate to false just prior to setting the DataSource and it works great.
Related
I cannot make single column to be editable in DataGridView. I tried to do it in the designer but didn't get positive results. I tried scripting it like
datagridview1.Columns["theColumn"].ReadOnly=false;
but that didn't work. Seems like the property of ReadOnly in DataGridView is only that is considering, no other modifications count. I actually try to make DataGridViewColumn that contains CheckBoxes editable, while other columns I get from DataBase.
You can easily do it through the Edit Column wizard of the grid view, Go to edit column windows and select the columns that you want to make read only and in the right pane just set the ReadOnly property to true.
Visual Studio did not allow me to set single column ReadOnly property false when Enable Editing property of DataGridView is unchecked(???). But HOKBONG's comment helped. Thanks guys.
I am using VS Express 2012 and SQL.
I have created a datagrid and used the designer to add the columns I want to display. This works fine. The column data gets entered into the correct column.
The problem is that the datagrid then goes and ads the raw SQL column names and dumps the data there as well.
How can I use datagrid.DataSource(dataTable) in a way that it only uses the columns I assigned in the designer?
Assuming you're developing for WinForms...
Before assigning the DataSource, try setting:
datagrid.AutoGenerateColumns = false
From MSDN:
Columns are automatically generated when this property is set to true and the DataSource or DataMember properties are set or changed.
Set the AutoGenerateColumns to false in the datagrid definition:
AutoGenerateColumns="false"
I have a datagridview in my form. This datagridview has some columns. There are some custom columns (I have created custom datagridview cells). These custom cells have some properties that I want to do visible from datagridview's columns editor in design time in order to set them. So in design time, I open datagridview's columns editor, and I create a column of the custom datagridview cell. Then, I set some custom properties and I close datagridview's olumns editor. When I open the datagridview's columns editor, the values that I set previously for those custom properties are not reflected, it seems like they were not saved once datagridview's columns editor was closed. So... why? why the values for the custom properties are not saved? What Am i doing wrong?
Furthermore, I cannot leave as empty these custom properties because an exception I raised once form is loaded (object reference not set as a instance of an object).
I highly appreciate if someone could help me.
I ran into this identical issue. After searching, I found some feedback on a microsoft site that said I had to implement iCloneable in my datagridviewtextboxcolumn derivation.
You can find the article here and the relevant section:
In some rare cases, the column type may want to expose a property that
has no equivalent property at the cell level. Examples of that are
DataGridViewLinkColumn.Text and DataGridViewImageColumn.Image. In
those cases, the column class needs to override the Clone method to
copy over that property.
My column added four extra properties, and here's my icloneable function:
//Override this method to set the custom properties.
public override object Clone()
{
var col = base.Clone() as BauerDataGridViewTextBoxColumn;
col.ShowBorder = this.ShowBorder;
col.BorderColor = this.BorderColor;
col.ColumnChooserIsOptional = this.ColumnChooserIsOptional;
col.ColumnChooserColumnLabel = this.ColumnChooserColumnLabel;
return col;
}
Again a c# question I cant't seem to figure out:
I have a datagridview on my form. In Visual Studio design mode, I enter 4 columns: refernce, country, name & city.
After a searchform is filled in, I try to refresh the datasource: A custom class fetches all the data from the selected table and fills the datagridview. The problem is: the datagridview now has all the columns of the table, and I only want the 4 columns entered in design mode.
I can put all the other columns on visible = false in design mode, and that works. But I want this datagridview as a custom control. So I only want to show the 4 entered columns, without disabling all the others. The data which is invisible is used to bind to a panel with other Controls like TextBoxes.
Does somebody know if this is possible, and how I should try to solve this?
Thanks!
You have three ways of hide columns.
1.- setting the visible property to false (as ksogor said) and don't create columns in design mode. Note that
GridView1.Columns["ColumnName"].Visible = false;
is more readable and mantenible that
GridView1.Columns[1].Visible = false;
2.- setting AutoGenerateColumns to false and create columns in the designer
3.- in your class, set the attribute [Browsable(false)] in the fields you don't want to show. Don't create columns in design mode.
The third way will hide the column in all the datagrids of your app. I love it.
[Browsable(false)]
public string Something{get;set;}
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.