Reputation: 3018
I am developing MyDataGridView control with fixed columns and rows.
public partial class MyDataGridView
{
public MyDataGridView()
{
CreateColumns();
CreateRows();
}
}
When I add MyDataGridView onto a Form at design-time the designer is autogenerating more columns - exactly the same columns as created with CreateColumns() in the constructor above. Can I stop the designer from doing this?
private void CreateColumns(); // Creates columns of MyDataGridView.
Please add this code to a WindowsFormsApplication project, compile and drag MyDataGridView control from ToolBox on a Form1 then run the application. See how the Designer behaves and generates code (Form1.Designer.cs).
namespace WindowsFormsApplication1
{
public class MyDataGridView : System.Windows.Forms.DataGridView
{
public MyDataGridView()
{
CreateColumns();
CreateRows();
}
private void CreateColumns()
{
for (int day = 0; day < 7; day++)
{
this.Columns.Add(new DataGridViewTextBoxColumn());
}
}
private void CreateRows()
{
for (int n = 0; n < 6; n++)
{
this.Rows.Add(new DataGridViewRow());
}
}
}
}
Upvotes: 1
Views: 262
Reputation: 341
Try this approach:-
First set AutoGenerateColumns="False"
and
cancelling the generation of the column if it's ColumnName equals to the value you can exclude, like
private void MyDataGrid_AutoGenerateColumn(
object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if ((string)e.Column.Header == "fieldname")
{
e.Cancel = true;
}
}
Upvotes: 1