Forced Column Widths in a DataGrid?

G

Guest

Hi. Is there any way to lock down the width of columns in a DataGrid so that
even if the text in one of the rows of that column is very wide, it just gets
cut off or something rather than causing the column to get wider?

The problem is that even if I set the column to wrap text, sometimes a
single "word" or string is very long without any spaces and therefore doesn't
wrap but just forces the column wider - and that's not what I want.

Any ideas?

PS: I've tried to go the the DataGrid managed group a bunch of times over
the past month and, at least on the MSDN web site, it always shows as empty
with no items. That's why I'm posting this here instead of there.

Thanks!

Alex
 
B

bruce barker \(sqlwork.com\)

if you read CSS documentation, you will see you can set the overflow
behavior, which is what you want to do.

-- bruce (sqlwork.com)
 
G

Greg9Strat

Yes, you can using something like the following in your control:

<Columns>
<asp:BoundField HeaderText="My Header" DataField="MyField"
HeaderStyle-Width="90px" />
</Columns>

That should force the column to be the width that you desire - by using
a unit measure of "px"... you can also use % to make it adjust to a
percentage of the window width...
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your post.

You may need to set the table-layout to 'fixed' to make sure fixed width
column working. For example:

protected void Page_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
ar.Add("ReallylonglinewithNoSpacesAtAll!!!!!!!!!! and
anotherverylonglinelonglinelongline");
grid1.DataSource = ar;
grid1.DataBind();
grid1.Style.Add("table-layout", "fixed");
}

protected void grid1_ItemDataBound(object sender, DataGridItemEventArgs
e)
{
//e.Item.Cells[0].Text = "<nobr>" + e.Item.Cells[0].Text +
"</nobr>";
e.Item.Cells[0].Attributes.Add("WIDTH", "75px");
e.Item.Cells[0].Attributes.Add("onmouseover",
"this.title=this.innerText;");
}

This will show a table cell that has 3 lines but the two long words gets
clipped. If you don't want it gets wrapped, uncomment the first statement
in ItemDataBound and only the first word gets shown and clipped.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top