document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. private void initializeColumnWidths()
  2. {
  3. //iterate through the columns in the grid
  4.     foreach (DataGridViewColumn Col in customerDataGridView.Columns)
  5.     {
  6. //if it\'s a text box column (this will skip dropdowns and checkboxes
  7.         if (Col is DataGridViewTextBoxColumn)
  8.         {
  9. //if the data type is string (not numeric or date)
  10.             if (Col.ValueType == typeof(System.String))
  11.             {
  12. //use the dataset on your form
  13.                 int maxlen = customerDataset.customer.Columns[Col.DataPropertyName].MaxLength;
  14. //optional: add the column width to the column header text
  15.                 Col.HeaderText += string.Format("({0})", maxlen);
  16. //Set the maxinputlength
  17.                 (Col as DataGridViewTextBoxColumn).MaxInputLength = maxlen;
  18.             }
  19.         }
  20.     }
  21. //this should be repeated for every grid on your form.
  22. }
');