Gridview Column Width

  • Thread starter Thread starter Yin99
  • Start date Start date
Y

Yin99

I have a Gridview binding to a DataTable source. I'd like to set the
column with of the second column. I cannot do this apparently because
when AutoGenerateColumns=true, they do not appear in the columns
collection.

I modified the RowCreated even to gridview, and I can change
properties on the cells, but not width. (setting tooltip, background
color, etc, all work but changing width has no effect). Here's
sample code I am using:

//Inside RowCreated Gridview Even
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell tCell in e.Row.Cells)
{
tCell.ControlStyle.Width = 600; //no effect
tCell.Width = 600; //no effect
}
}

So the main question is, "Any way possible to change Gridview Column
Width with a DataTable source and AutoGenerateColumns=true?" Please
Help, I am at a loss here and been working on this for days....

-Yin

PS
little more background- I have a dynamic DataTable (could be 1 to
over 3000 columns), so I must have AutoGenerateColumns=true (I can't
set it to false, and create a boundfield for an unknown dynamic sized
table).
 
<< little more background- I have a dynamic DataTable (could be 1 to
over 3000 columns), so I must have AutoGenerateColumns=true (I can't
set it to false, and create a boundfield for an unknown dynamic sized
table) >>

you're kidding, right? up to 3000 columns? .... this is software ... to be
used by humans ... not alchemy

leaving that aside (which you should not) it doesn't seem advisable to alter
column width every time a row is created; have you considered all the work
for the processor you're creating here .... I hate to ask how many rows
there may be in the control ...
 
<< little more background- I have a dynamic DataTable (could be 1 to
over 3000 columns), so I must have AutoGenerateColumns=true (I can't
set it to false, and create a boundfield for an unknown dynamic sized
table) >>

you're kidding, right? up to 3000 columns? .... this is software ... to be
used by humans ... not alchemy

leaving that aside (which you should not) it doesn't seem advisable to alter
column width every time a row is created; have you considered all the work
for the processor you're creating here .... I hate to ask how many rows
there may be in the control ...









- Show quoted text -

nope, it's a timespread and not really processor intensive. but still
the question remains, why in the world
can I not change a column width to a DataTable source with
AutoGenerateColumns=true?
Woudln't this be something a lot of people would want to do?

I can't imagine the only answer is to turn AutoGenerateColumns=false,
and add a bound field, map this
to the datatable, etc. Please, anyone out there know the answer?
Thanks!
 
i was able to make a workaround by adding a bunch of spaces to the
header row column but
still unable to get the .Width property to explicitly change the
column width on header row or
datarows.

here's sample code:

if (e.Row.RowType == DataControlRowType.Header)
{
if (e.Row.Cells[1].Text == "Column Header Text")
{
e.Row.Cells[1].Wrap = false;
e.Row.Cells[1].Width = Unit.Percentage(1000);
e.Row.Cells[1].Text = "Column Header Text " +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
 
i was able to make a workaround by adding a bunch of spaces to the
header row column but
still unable to get the .Width property to explicitly change the
column width on header row or
datarows.


well ... I fooled around with this and I can get .Width property to
"influence" the width of the cell; if I set it to 5, it'll make it as small
as possible, and if I set it to 2000, it'll make it as wide as possible ...
but it will not just use the metric supplied absolutely

but here's the thing: if you look at the HTML source you should see:

<td style="width:2000px;">

if you set your Width to 2000, for example; so the code does what you ask
it to but the browser (IE6 in this case) just renders it as it "wants to"
.....


here's sample code:

if (e.Row.RowType == DataControlRowType.Header)
{
if (e.Row.Cells[1].Text == "Column Header Text")
{
e.Row.Cells[1].Wrap = false;
e.Row.Cells[1].Width = Unit.Percentage(1000);
e.Row.Cells[1].Text = "Column Header Text " +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
 

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

Back
Top