Column Count Error in datagridview

OmkarHcl 206 Reputation points
2023-10-08T11:32:22.53+00:00
public partial class sale_purchase_datagridview : DataGridView
    {
        private SqlConnection con;
        private SqlDataAdapter da;
        private DataTable dt, dt2;
        public sale_purchase_datagridview()
        {
            this.Columns[5].ReadOnly = true;
            InitializeComponent();
           
        }
        protected override void OnEnter(EventArgs e)
        {
            
            base.OnEnter(e);
           

        }


t User's image

The problem is what i want is that , the column 5 of sale_purchase_datagridview must always be readonly . Although there is one thing due to this the error is popping , i have not add the columns in this custom datagridview itself . I have added the columns in the form where i am using this control . So is there a way by which i can achieve this ?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,927 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,429 questions
{count} votes

Accepted answer
  1. KOZ6.0 6,580 Reputation points
    2023-10-08T12:27:01.52+00:00
    protected override void OnColumnAdded(DataGridViewColumnEventArgs e) {
        base.OnColumnAdded(e);
        if (e.Column.Index == 5) {
            e.Column.ReadOnly = true;
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.