467,923 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Change GridVIew row textbox width in edit mode

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 <tagof 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[i];
bc.ReadOnly=(readonlyflags[i]=="1");
bc.ApplyFormatInEditMode=true;
bc.ItemStyle.Width=Unit.Pixel((i+1)*100);
this.DataGrid.Columns.Add(bc);
}
}
}

}

May 5 '07 #1
  • viewed: 14580
Share:
1 Reply
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[i];
bc.ReadOnly = (readonlyflags[i] == "1");
this.DataGrid.Columns.Add(bc);
}
}
}
else
{
for (int i = 1; i < this.DataGrid.Columns.Count; i++)
{
DataControlField dc =
this.DataGrid.Columns[i];
BoundField bc = dc as BoundField;
if (bc != null)
{
int chars = i * 25;
bc.ControlStyle.Width = Unit.Pixel(chars *
6);
}
}
}

May 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Robert Smith jr. | last post: by
reply views Thread by jeffmagill | last post: by
reply views Thread by Mike P | last post: by
2 posts views Thread by VB Programmer | last post: by
reply views Thread by Mike P | last post: by
1 post views Thread by Evan M. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.