This is a migrated thread and some comments may be shown as answers.

Setting MaxLength for Autogenerated Edit and Insert Text Boxes

0 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marbry
Top achievements
Rank 1
Marbry asked on 19 Sep 2011, 03:13 PM
This gave me fits, perhaps it can be useful to someone else.

The problem was that I have one page where the user can select from a drop down to edit one of a number of different reference tables in the grid.  They select a table, and the datasource and grid are rebound to that particular table.

Part of the problem then is limiting their input to the proper length for each field.

Unfortunately the sqldatasource doesn't preserve the underlying SQL types, at least not that I could find.  It converts varchars to a "string" type.  So there is no field length available.

So we retrieve the schema data separately from the DB.  Where lstReference is the list box they select the table to edit from, and the value contains the table name.  Note that it's only getting the length for the varchar and nvarchar fields.  Those are the only ones I'm interested in here, but you could certainly modify that to your needs.
private void GetSchema()
{
    // Clear the list out.
    _charFieldLength.Clear();
    DataTable dt = new DataTable();
    string connString = Util.GetConnectionString();
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();
      
    try
    {
        // Retrieve the schema info from the DB.
        string[] restriction = { Util.GetDBName(), null, lstReference.Items[lstReference.SelectedIndex].Value, null };
        dt = conn.GetSchema("Columns", restriction);
    }
    catch (Exception ex)
    {
        // Log error
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
    }
    // Populate the list with the schema info we're interested in.
    foreach (DataRow row in dt.Rows)
    {
        if (row["DATA_TYPE"].ToString() == "varchar" || row["DATA_TYPE"].ToString() == "nvarchar")
        {
            FieldInfo fi = new FieldInfo();
            fi.FieldName = row["COLUMN_NAME"].ToString();
            fi.DataLength = int.Parse(row["CHARACTER_MAXIMUM_LENGTH"].ToString());
            _charFieldLength.Add(fi);
        }
    }
}


And we can store that information in a list of these structs, just the field name and length.
private struct FieldInfo
{
    public string FieldName;
    public int DataLength;
    public FieldInfo(string fieldName, int dataLength)
    {
        FieldName = fieldName;
        DataLength = dataLength;
    }
}
List<FieldInfo> _charFieldLength = new List<FieldInfo>();


And this took me a while to weed out, but I did find a Telerik example that I was able to finally use to get a reference to the auto generated edit text boxes to be able to actually set the MaxLength.  This works for both Edit and Insert.
protected void gridActivity_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem form = (GridEditableItem)e.Item;
        for (int i = 0; i < _charFieldLength.Count; i++)
        {
            TextBox dataField = (TextBox)form[_charFieldLength[i].FieldName].Controls[0];
            dataField.MaxLength = _charFieldLength[i].DataLength;
        }
    }
}

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Marbry
Top achievements
Rank 1
Share this question
or