problem sizing columns in datagrid

M

Mad Scientist Jr

I have a datagrid that has a column that I would like to be the widest
(say 60%) - however another column is populated with some data that is
sometimes a rather long string, and it expands to take up most of the
table width. I tried to force the column sizes to force the longer
column to wrap but it isn't working. Any help appreciated, my code is
below:

DataGrid1.DataSource = DataTable1
DataGrid1.DataBind()
DataGrid1.Columns(0).ItemStyle.Width = Unit.Percentage(10)
DataGrid1.Columns(1).ItemStyle.Width = Unit.Percentage(60)
' this column is getting squashed
DataGrid1.Columns(2).ItemStyle.Width = Unit.Percentage(10)
' this column still ends up taking 70% or so
DataGrid1.Columns(3).ItemStyle.Width = Unit.Percentage(10)
 
A

anon

One solution is to check the length of this particular column that is
causing wider columns in itemdatabound....

string d = Convert.ToString(DataBinder.Eval(e.Item.DataItem,....));
if (d.Length > 50)
{
Label lb = (Label) e.Item.FindControl("lb");
lb.Text = d.Substring(0, 45) + "...";

}

Hope that helps,
Ben
 
M

Mike Hutton

anon said:
One solution is to check the length of this particular column that is
causing wider columns in itemdatabound....

string d = Convert.ToString(DataBinder.Eval(e.Item.DataItem,....));
if (d.Length > 50)
{
Label lb = (Label) e.Item.FindControl("lb");
lb.Text = d.Substring(0, 45) + "...";

}

Hope that helps,
Ben

I have a related problem, but one which I can't really use substrings
on.

I have a column which has generated HTML tags in it. The tag is a link
to a javascript function which needs to be called with the key value
of the row, and also has a JPG embedded in it. I want to be able to
use this as my grid needs to be dynamic - I don't always want to
display this column.

As you can guess, the DataGrid is resizing my grid so that the column
takes up space commensurate with the size of the text which generates
the link, rather than the image it displays.

Is there any way I can completely override the column width so it
doesn't "correct" it in this way?

Mike.
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top