Change GridVIew row textbox width in edit mode

Discussion in 'ASP .Net' started by savajx1, May 5, 2007.

  1. savajx1

    savajx1 Guest

    I need to dynamically create a set of bound fields contained in a
    GridView control. I also have a single static CommandField that I can
    declare in the Columns <tag> of the GridView control. I have to add
    controls dynamically as I am trying to write a reusable , general ,
    spreadsheet like display control that can take advantage of the built-
    in update and delete features of the SqlDataSource/Gridview controls.
    This user control can get its "field name and width settings" from a
    configuration file (actually from the settings properties of a custom
    DotNetNuke module)

    The problem is that when I click on the edit/update command , and go
    into the Gridview's editmode, the textboxes displayed in each cell can
    be way to small in width. I need to control the width of the textbox
    displayed in each column for each field that is not readonly but can't
    figure out how to get a reference to each textbox so that I can set
    the width property. I want to avoid having to dynamically create
    "templated columns" but I fear that this is the only solution.

    The Gridview itself doesn't appear to be handling this properly as the
    textbox widths don't appear related to the column widths unless I am
    somehow overriding its default behavior. You can see my feeble
    attempt to control the "edit mode" width of the textbox by controlling
    the ItemStyle.width below.

    Has anyone else figured out how to do this??????


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    if ((Settings[DisplayFieldsProperty] != null) &&
    (Settings[DisplayFieldReadOnlyFlags] != null))
    {
    string[]
    fields=Settings[DisplayFieldsProperty].ToString().Split(semicol);
    string[]
    readonlyflags=Settings[DisplayFieldReadOnlyFlags].ToString().Split(semicol);
    if (fields.Length != readonlyflags.Length)
    { throw new
    System.IO.InvalidDataException("Mismatch on field
    settings"); }
    for(int i=0; i < fields.Length; i++)
    {
    BoundField bc = new BoundField();
    bc.DataField=fields;
    bc.ReadOnly=(readonlyflags=="1");
    bc.ApplyFormatInEditMode=true;
    bc.ItemStyle.Width=Unit.Pixel((i+1)*100);
    this.DataGrid.Columns.Add(bc);
    }
    }
    }

    }
    savajx1, May 5, 2007
    #1
    1. Advertising

  2. savajx1

    savajx1 Guest

    I discovered a solution. Anyone who had worked with the gridview for
    any length of time (which I haven't) would probably know this. As it
    took me all day to discover this I'll post a solution here. This
    appears to work. Just set the ControlStyle property of the BoundField
    when it is NOT A POSTBACK, as shown below. Just wish that I had read
    that ControlStyle definition earlier. I'd still like to know if there
    is a better way


    if (!Page.IsPostBack)
    {
    string headerText = String.Empty;
    // Check the settings to see if this module is using
    Personalization for vote tracking
    if (!(Settings[HeaderTitleProperty] == null))
    {
    headerText =
    Settings[HeaderTitleProperty].ToString();
    headerText = this.ModuleId.ToString();
    }
    else headerText = this.ModuleId.ToString();
    this.HeaderLabel.Text = headerText;
    if ((Settings[DisplayFieldsProperty] != null) &&
    (Settings[DisplayFieldReadOnlyFlags] != null))
    {
    string[] fields =
    Settings[DisplayFieldsProperty].ToString().Split(semicol);
    string[] readonlyflags =
    Settings[DisplayFieldReadOnlyFlags].ToString().Split(semicol);
    if (fields.Length != readonlyflags.Length)
    { throw new
    System.IO.InvalidDataException("Mismatch on field settings"); }
    for (int i = 0; i < fields.Length; i++)
    {
    BoundField bc = new BoundField();
    bc.DataField = fields;
    bc.ReadOnly = (readonlyflags == "1");
    this.DataGrid.Columns.Add(bc);
    }
    }
    }
    else
    {
    for (int i = 1; i < this.DataGrid.Columns.Count; i++)
    {
    DataControlField dc =
    this.DataGrid.Columns;
    BoundField bc = dc as BoundField;
    if (bc != null)
    {
    int chars = i * 25;
    bc.ControlStyle.Width = Unit.Pixel(chars *
    6);
    }
    }
    }
    savajx1, May 6, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yunus Emre ALPÖZEN [MCAD.NET]
    Replies:
    1
    Views:
    699
  2. AndrewF
    Replies:
    1
    Views:
    740
    Bruce Barker
    Oct 10, 2005
  3. K Schleyer

    Can I programmatically change width/height of column in edit mode

    K Schleyer, Jan 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    142
    K Schleyer
    Jan 16, 2004
  4. Sharon McCarty
    Replies:
    0
    Views:
    241
    Sharon McCarty
    Nov 24, 2004
  5. Jaime Stuardo

    GridView control enters edit mode when I click Edit link twice

    Jaime Stuardo, Apr 7, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    377
    Jaime Stuardo
    Apr 7, 2006
Loading...

Share This Page