We help IT Professionals succeed at work.

How to set datagridview column MaxInputLength at runtime

dave_sky
dave_sky asked
on
3,489 Views
Last Modified: 2013-11-26
Using a databound datagridview.
How can I set a datagridview column's MaxInputLength at runtime with a button click event?
Sample code would be appreciated.
Comment
Watch Question

Top Expert 2007

Commented:
In the button click event handler, assuming the column index is k

dataGridView1.Columns[k].MaxInputLength = newValue;

Jim

Author

Commented:
Jim,
This does not work:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
1. Me.TblUpgradesDetailDataGridView.Columns("BasicDescription").MaxInputLength = 300
2. Me.TblUpgradesDetailDataGridView.Columns("DataGridViewTextBoxColumn4").MaxInputLength = 300

I think this would work if I knew how to adapt the code:
 Dim col As New DataGridViewTextBoxColumn
      col.MaxInputLength = 300
Top Expert 2007

Commented:
When you define the column, and add rows, the cell properties take on the attributes of the column for that cell. So, apparently, that doesn't cascade when  you change the column. Other properties do, so it is difficult to predict which is which. That means you will have to iterate over the rows, and set the MaxInputLength property for the cell in each row in that column.

Jim

Author

Commented:
Jim,
Do you have time to give me some basic sample code. I have tried this with not luck.

Thanks
Top Expert 2007

Commented:
What classes did you use to define the column and cell templates? Is it DataGridViewTextBoxColumn and DataGridViewTextBoxCell?

Jim
 

Author

Commented:
Jim,
Yes, that's correct.
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2007

Commented:
By the way - you need to set the value of index so you are looking at the appropriate column.

Jim
Top Expert 2007

Commented:
One more thought: If you allow the user to add rows to the grid, you need to set that property for the column as well.

Jim

Author

Commented:
Jim,
That did the trick!

Thank you
Top Expert 2007

Commented:
My pleasure. Good luck!

Jim

Commented:
Jim your solution is useful right now!

Thank you
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.