none
How to set max length of datagridview column?

    Question

  • Hi to all,

     

    I have a datagridview on form in window application. I want to restrict the length of the characters of the columns of a datagridview.

    Please tell me.

     

    Thanks

    Sandeep Soni

    Friday, January 04, 2008 10:31 AM

Answers

  • Hi, Sandeep Soni,

     

    Based on my understanding, you want to limit the maximum input length of the cells in your DataGridView, don't you?

     

    Actually, we can do this in EditingControlShowing event.

    For example

    Code Block

            private void Form1_Load(object sender, EventArgs e)

            {

                DataTable datatable = new DataTable();

                datatable.Columns.Add("col1");

                datatable.Columns.Add("col2");

                datatable.Rows.Add("ABC", "bcd");

                datatable.Rows.Add("bcd", "ABC");

     

                dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

                dataGridView1.DataSource = datatable;

            }

     

            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {

                if (e.Control is TextBox) //If it is a DataGridViewTextBoxCell

                {

                    (e.Control as TextBox).MaxLength = 4; //Set the MaxLength to 4

                }

            }

     

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx

     

    Hope this helps,

    Regards

    Tuesday, January 08, 2008 5:29 AM

All replies

  • No direct way to do that, bu you can sublcass it and override its CellFormatting event. In this event, based on the column, you could format the cell/column contents in any way.
    See this example here http://msdn2.microsoft.com/en-us/z1cc356h.aspx

    Regards
    Friday, January 04, 2008 12:27 PM
  • Hi, Sandeep Soni,

     

    Based on my understanding, you want to limit the maximum input length of the cells in your DataGridView, don't you?

     

    Actually, we can do this in EditingControlShowing event.

    For example

    Code Block

            private void Form1_Load(object sender, EventArgs e)

            {

                DataTable datatable = new DataTable();

                datatable.Columns.Add("col1");

                datatable.Columns.Add("col2");

                datatable.Rows.Add("ABC", "bcd");

                datatable.Rows.Add("bcd", "ABC");

     

                dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

                dataGridView1.DataSource = datatable;

            }

     

            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {

                if (e.Control is TextBox) //If it is a DataGridViewTextBoxCell

                {

                    (e.Control as TextBox).MaxLength = 4; //Set the MaxLength to 4

                }

            }

     

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx

     

    Hope this helps,

    Regards

    Tuesday, January 08, 2008 5:29 AM
  • Hello all,

     

    I know this is coming 3 years too late!  However, since I just found this post and found a good solution to the problem in the same afternoon, I might as well give my answer.  

     

    The only "trick" is to cast the DataGridViewColumn into the concrete type you need, which I assume is the TextBoxColumn.  E.g. 

     

     

    CType(DataGridView1.Columns("MyColumn"), DataGridViewTextBoxColumn).MaxInputLength = 50
    
    

     

    This can be done whether you are instantiating/adding the columns yourself or using databinding with [.AutoGenerateColumns = True] and simply editing the characteristics after the bind. 
    Hope someone finds this useful.

     


    • Proposed as answer by Eddie Blattner Saturday, October 13, 2012 5:26 PM
    Friday, March 18, 2011 10:35 PM
  • Hello all,

     

    I know this is coming 3 years too late!  However, since I just found this post and found a good solution to the problem in the same afternoon, I might as well give my answer.  

     

    The only "trick" is to cast the DataGridViewColumn into the concrete type you need, which I assume is the TextBoxColumn.  E.g. 

     

     

    CType(DataGridView1.Columns("MyColumn"), DataGridViewTextBoxColumn).MaxInputLength = 50
    
    

     

    This can be done whether you are instantiating/adding the columns yourself or using databinding with [.AutoGenerateColumns = True] and simply editing the characteristics after the bind. 
    Hope someone finds this useful.

     


    This worked like a charm, thanks!!
    Saturday, October 13, 2012 5:27 PM
  • Another 3 years on and I've just found this brilliant simple solution. Thank you LLamaBrett
    Saturday, June 07, 2014 5:38 PM