Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to set maxlength of a column of a datagridview in C#.Net?
 

How to set maxlength of a column of a datagridview in C#.Net?

I am developing a windows application in C#.Net 2005.
In this I have used one datagridview towhich I bound one datatable whichhas three columns.
In thisone column called as Emp Name is editable i.e. user can enter \ updateEmp name.
But I want to retrict Emp Name to 50 characters.
I want to set maxlength of that column at RUNTIME. I know there is property called MaxInputLength atdesigntime but I am not getting such property at runtime.
Datagrid name is dgrdChannel and that column No. is 2

I tried - dgrdChannel.Columns[2].MaxInputLength

But there is no such property.
So please give me code for this.


And also Only letters should be entered in that column and no numbers or special characters.
I know how to validate this as -
if (Char.IsLetter(e.KeyChar) == true )

But on which event I should write this, There is no event such as CellKeyPress.

How to do this?

Harsh1  Friday, January 18, 2008 11:29 AM

The sample in this FAQ is eactly what you need:
8.How to display grouped data in a DataGridView?

Zhi-Xin Ye  Tuesday, January 22, 2008 12:07 PM

I solved problem of setting maxlength as -

DataGridViewTextBoxColumn cName = (DataGridViewTextBoxColumn)dgrdChannel.Columns[2];

cName.MaxInputLength = 50;

But I am not able to solve problem of data validation.

Also I have one more query -

I have used one moredatagridview in which i want to show a tree view.
e.g.
Suppose i have following table -
Department EmployeeName Salary
Finance deep 10000
Finance praful 20000
Finance xyz 10000
Marketing abc 20000
Marketing pqr 20000
Marketing lmn 20000


This table if I bind to the datagridview then it should look like this-
Department EmployeeName Salary
Financedeep 10000
praful 20000
xyz 10000
Marketing abc 20000
pqr 20000
lmn 20000

i.e. Department Name should not be repeated.
And if that department name is in bold then it will be great.

Please give me the code to do this?

Harsh1  Monday, January 21, 2008 9:53 AM

The sample in this FAQ is eactly what you need:
8.How to display grouped data in a DataGridView?

Zhi-Xin Ye  Tuesday, January 22, 2008 12:07 PM

Please solve my problem of validation as mentioned in my first post -

Only letters should be entered in that column and no numbers or special characters.
I know how to validate this as -
if (Char.IsLetter(e.KeyChar) == true )

But on which event I should write this, There is no event such as CellKeyPress.

Harsh1  Tuesday, February 05, 2008 1:43 PM
Zhi-Xin Ye  Tuesday, February 05, 2008 1:52 PM

You can use google to search for other answers

Custom Search

More Threads

• creating custom events for combo box
• Showing a Collection Editor from a Smart Tag
• Groupbox...missing properties (text foreground & text justification)
• Why DataGridView behaves like this?
• Adding properties to a ToolStripButton with an IExtenderProvider.
• Tips for fixing form designer loading errors - MUST READ esp. MSFT
• Using commands/verbs in the PropertyGrid when hosting the Form Designer
• Controls are not moving after dropped on the Designer
• member field for custom component not getting added to form
• Designer Fails to Show From after VS 2003 Port