• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3197
  • Last Modified:

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.
0
dave_sky
Asked:
dave_sky
  • 7
  • 4
1 Solution
 
JimBrandleyCommented:
In the button click event handler, assuming the column index is k

dataGridView1.Columns[k].MaxInputLength = newValue;

Jim
0
 
dave_skyAuthor 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
0
 
JimBrandleyCommented:
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
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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

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

Jim
 
0
 
dave_skyAuthor Commented:
Jim,
Yes, that's correct.
0
 
JimBrandleyCommented:
Here it is in C# (I am not really a VB guy):
int index = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
   DataGridViewTextBoxCell cell = row.Cells[index];
   cell.MaxInputLength = newValue;
}

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

Jim
0
 
JimBrandleyCommented:
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
0
 
dave_skyAuthor Commented:
Jim,
That did the trick!

Thank you
0
 
JimBrandleyCommented:
My pleasure. Good luck!

Jim
0
 
fantamenCommented:
Jim your solution is useful right now!

Thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now