Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

How to set datagridview column MaxInputLength at runtime

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.
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

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

dataGridView1.Columns[k].MaxInputLength = newValue;

Jim
Avatar of David Svedarsky

ASKER

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
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
Jim,
Do you have time to give me some basic sample code. I have tried this with not luck.

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

Jim
 
Jim,
Yes, that's correct.
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
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
By the way - you need to set the value of index so you are looking at the appropriate column.

Jim
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
Jim,
That did the trick!

Thank you
My pleasure. Good luck!

Jim
Jim your solution is useful right now!

Thank you