Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid - width of a column

Status
Not open for further replies.

lsmyth1717

Programmer
Mar 25, 2005
44
GB
I have a datagrid and i'm trying to fix the width of one of the columns to be exactly 300px. My problem is that one of the records fields(CompanyName) is really long and has no spaces so it streches the whole datagrid accross the screen. Has anyone else ever experienced this problem and is there any way of fixing it. Basically my datagrid is streching to about 120% because of this one record. Would love to work out how I can change this setting. Any suggestions or help at all would be appreciated.

EXAMPLE CODE:
<asp:Table Width="500px" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell Width="500px">
<asp:DataGrid id="MyDataGrid" runat="server" Width="450px" AllowPaging="True" AllowSorting="True" PageSize="5" AutoGenerateColumns="false" DataKeyField="SupplierID" AllowCustomPaging="True" ShowFooter="True">
<Columns>
<asp:BoundColumn Visible="False" DataField="SupplierID" HeaderText="SupplierID">
<HeaderStyle Height="20px" Width="60px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" ReadOnly="True" HeaderText="Company Name">
<HeaderStyle Height="20px" Width="300px"></HeaderStyle>
</asp:BoundColumn>
 
Maybe you could try saving CompanyName so it will format.

But to be honest, if it is that long, how do you want it formatted?
 
Unfortunatley I have no control over the CompanyName field so I can't make the user put spaces in the string if that what you meant by saving CompanyName so as it will format.

I want it formatted so as it doesn't stretch the datagrid column really wide. If there were spaces in the value of the CompanyName field then it displays ok as it takes a line break were the spaces are.

Really stuck with this one. Any more ideas or suggestions?
Cheers for your help.
 
I think we need a more detailed example of what you want. You say you have no control over the field, but do you mean that you can't change what the user inputs, or that you can't change it when you query it from the DB? You say you don't want the datagrid stretched, so do you want the company name truncated? If you want the whole company name in the cell, but can't manipulate it, I don't see how there is a solution. If you want it truncated, you can do it in the sql query or on the page. Let us know some specifics. Thanks.
 
What do you mean by truncated? How do you do this on the page.
I am allowed to make the entry spread over lines and add spaces in it dynamically if i knew how.

I can't change what the user inputs but I can display a long entry with no spaces over a couple of lines so as the datagrid doesn't stretch. However I don't know how to do this. An example of the entry which is giving me a problem is: -
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

An entry like this without spaces stretches the datagrid column accross the pages and messes everything up. Any ideas of how I can get around this would be really appreciated. Hope i've given you enough information.

Basically if the above entry was the following below I wouldn't have a problem because there are spaces and the datagrid column remains the same size as I set it.

This same entry with spaces wouldn't cause problems
aaaaaa aaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aa aaa aaaaaa aaaaaaaaaa aaaaaa aaaaaaaaa aaaaaaaaaaaa aaaa aaa aaaaaaaaaa aaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaa aaaaaa aaaa aa aaaa

Cheers for all your help and effort helping me
 
One thing that I've done that seems to work better is, instead of setting the width on the header, I go into the HTML code and set the width of the Item...removing the width property of the Header.

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Personally I would go with the Truncate in your SQL statement like henslecd originally suggested (i.e only take the first 50? characters).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Truncate means shorten to a specified length.

If your companyName is aaaaaaaaaaaaaaaaaaa and you truncate it to only 3, the companyname displayed would be aaa.

However, a better question is, did you make the user input form? If so, why not only a certain length to company name? It doesn't seem logical or a solid system if someone can put in as long a string as they want for something as simple as company name. You can set it to where they can only enter in X amount of charachters from the front end, and also reenforce it on the DB end by setting the length value.

Please let us know if you can change the user input page and DB.

 
Sorry its taken so long to get back to everyone. Firstly the CompanyName was only an example it could be something like this reference. I know its still but sometimes a user may want to enter a really long reference. My problem is highlighted above perfectly as the really long "aaaa...." string I showed as an example stretched the width of the datagrid containing our posts and now means we need to scroll horizontally in order to view the whole post. I'm sure you'll agree this is very frustrating.

To get around my specific problem I did this in the html and code behind and now it only displays the first 40 characters.

<asp:TemplateColumn HeaderText="Your Ref" SortExpression="Your_Ref">
<HeaderStyle Height="20px" ForeColor="White" Width="150px" CssClass="dgHeader"></HeaderStyle>
<ItemTemplate>
<%#getYour_Ref(DataBinder.Eval(Container.DataItem,"Your_Ref").ToString())%>
</ItemTemplate>
</asp:TemplateColumn>

Code Behind
public string getYour_Ref(string p_sFieldValue )
{
int iLengthToUse = 0;

if (p_sFieldValue == DBNull.Value.ToString())
{
return string.Empty;
}

else if(iLength > 40)
{
iLengthToUse = 40;
string sYourRef = p_sFieldValue.Substring(0,iLengthToUse);
return sYourRef;
}

else
{
return p_sFieldValue;
}
}

Ideally I'd like to work with this more so as it returns the first 40 characters and also returns a tooltip which contains the full value that can be viewed the user hoovers over the datagrid cell.

Have you any idea how I could do this. Here is an idea of what I need to do.

//In here I basically would like to only return the first
//50 characters of the string and then for the rest of the
//string I’d like the details to be displayed in a tooltip
//for that cell. I know the below code won’t work because
//of the e but its just to give an idea of what I need to do.
//If someone knows how I can display a tooltip for the rest
//of the string I’d be really really grateful
//I’m not sure if this can be done but if anyone has any
//ideas I’d really appreciate it. Thanks
/*
iRemainder = p_sFieldValue.Length - 50;
string sTooltipText = p_sFieldValue.Substring(50,iRemainder);
if(e.Item.ItemType == ListItemType. SelectedItem)
{
e.Item.Cells[1].ToolTip = sTooltipText;
}
*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top