Question: i am new to C# and currently working on a datagrid view which got 3 columns , I am dynamically creating rows by reading a config file . My need is to limit the number of inputs in each column. Maximum input length will change for each . Say Row 1 , column 3 have 6 as max length , Row 2 , column 3 have 12 as max length. I tried to cast this dataGridView1.Rows[i].Cells[2] to DataGridViewTextBoxColumn but getting casting exception . I cannot set max input length through designer because the requirement is make it dynamically . 

Please help

Thanks
Nibin

Answer: 
in the RowDataBound or page load 

For DataGrid

```
protected void dgv1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    ((TextBox)e.Item.FindControl("ControlName")).MaxLength = 6;
}

```

For GridView

```
((TextBox)gridview1.Rows[0].Cells[2].FindControl("ControlName")).MaxLength = 6;

```

or

```
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{            
    ((TextBox)e.Row.FindControl("ControlName")).MaxLength = 6;
}

```
```
dataGridView1.Rows[i].Cells[2].MaxInputLength = x;

```

Look [here](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewtextboxcell(v=vs.110).aspx) for more details