Change GridVIew row textbox width in edit mode

S

savajx1

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);
}
}
}

}
 
S

savajx1

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);
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top