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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the datagrid to truncate 2

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I have some customer names that are long, and would like the datagrid to just chop them off. Any way to do this? I'm using grid setup and the datagrid overflows onto my controls below it. I tried no wrap, and it just makes the grid wider. This grid seems to be more trouble then it's worth.
 
You can modify them on the ItemDataBound. example

Private Sub dgDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgDataGrid.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblField = CType(e.Item.FindControl("lblField "), Label)
lblField.text = left(lblField.text, 10)
end if
end sub
 
I get this:

System.NullReferenceException: Object variable or With block variable not set.

This is on a display. Here is my code:

Private Sub dgAgwayCust_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAgwayCust.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblField = CType(e.Item.FindControl("CustName1"), Label)
lblField.text = Left(lblField.text, 10)
End If
End Sub
 
Hmm.

I pulled the example from live code (changed the names to protect their identity :) ).

Did you get a line number with the error? Are there values in all the rows of the datagrid for that field?

 
Yes. I guess my problem is that the datagrid grown both in length and width. I'm used to VB where it stays where you put it. This one has a mind of it's own.

I have controls under it. How do you get it to not grow?
 
The line the error is on is 112:
lblField.text = Left(lblField.text, 10)

It can't find CustName1, but that is the name of the column in the grid.
 
I am not certain of what is going on. I dont have time to dig into right now. you may want to check out or aspnet.4guysfromrolla.com both have excellant resources for datagrids.

hth
 
ok. If you are not using the edit template this should work for a reference e.Item.Cells(1).Text ...
 
I'm going to look into it too, but your last post worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top