I made a custom DataGridView component which has a standard DataGridViewImageColumn inside. A new property changes the visibility of the column when i don't need in in a particular table. I add the column in the constructor and update it on CellFormatting event. That is the part working like intended.
When I drop the control onto a new form it shows up with the new column in it. Running the program results in two image columns in the grid.
A new form just added the component and set Dock.Fill
When i start it without changing anything it shows me two columns. The first is working like it should and the second one always shows the missing x image (Had no data in it so they show both the x).
In the designer of the form there is a ne image column added automatically.
private CustomDataGridView customDataGridView1;
private System.Windows.Forms.DataGridViewImageColumn dataGridViewImageColumn1;
When i keep editing in the form it sometimes happens that VS creates even more copies of the same columns. To fix the problem i have to clear the DGV.Columns listing every now and then.
How can i prevent VS from copying my fields?
The following code is the minimal part to reproduce the problem.
public class CustomDataGridView : DataGridView
{
private DataGridViewImageColumn EditStatusIcons;
private bool hasIcons = true;
public bool HasIcons
{
get { return this.hasIcons; }
set
{
if (this.Columns["EditIcons"] == null) return;
this.Columns["EditIcons"].Visible = value;
this.hasIcons = value;
}
}
public CustomDataGridView()
{
this.EditStatusIcons = new System.Windows.Forms.DataGridViewImageColumn();
this.EditStatusIcons.HeaderText = "";
this.EditStatusIcons.Name = "EditStatusIcons";
this.Columns.Add(this.EditStatusIcons);
}
}
Edit: I also tried to use this.AutoGenerateColumns = false;
like in Custom DataGridView adds columns everytime I build said. Nothing changes.
Clearing the columns listing on the form removes the working column and leaves a column dataGridViewImageColumn1 which is just a complete empty column with the wrong name.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…