Link to home
Start Free TrialLog in
Avatar of pachecosita
pachecositaFlag for United States of America

asked on

Setting maximum Input lenght in Datagridview

How Do I set up maximum INPUT length programatically in datagridview (VB.NET)?

I am populating the Datagridview with a dataset
Avatar of jppinto
jppinto
Flag of Portugal image

Avatar of ROMA CHAUHAN
Use can use this code to set the max length
DataGridViewTextBoxEditingControl TextBoxControl;
 
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBoxControl = e.Control as DataGridViewTextBoxEditingControl;
            if (TextBoxControl != null)
            {
                TextBoxControl.KeyPress += TextBoxCellEditControlKeyPress2;
            }
        }
        private void TextBoxCellEditControlKeyPress2(object sender, KeyPressEventArgs e)
        {
            TextBoxControl.MaxLength = 3; 
        }

Open in new window

You can set the MaxInputLength property of datagridview column like this.

((DataGridViewTextBoxColumn)this.dataGridView1.Columns[0]).MaxInputLength = 10;
ASKER CERTIFIED SOLUTION
Avatar of pachecosita
pachecosita
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial