1

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());
            }
        }
    }
}
3
  • Answering would be easier if you posted a bit more of your code. Commented Jan 19, 2017 at 10:10
  • The problem is probably in your eventhandlers, check your forms Load eventhandler. Commented Jan 19, 2017 at 10:19
  • And to continue on @PaulKertscher´s input: there is always more code to show ;) Commented Jan 19, 2017 at 10:20

1 Answer 1

1

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;
        }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.