?
Solved

ASP.NET - Determining the detailsview title column width at runtime

Posted on 2010-11-12
5
Medium Priority
?
942 Views
Last Modified: 2012-05-10
Hello Experts,

I have a DetailsView object which pulls its data from a sqldatasource.  At runtime, based on the data retrieved, the page will hide some rows and show others of the DetailsView object.

Because of the hiding/showing of rows is based on data, the column width of the title column changes at runtime.  I need to be able to determine programatically what the column size is in pixels.

This doesn't work, but I'm thinking something like this:
Dim intWidth as integer = dvResults.Rows(0).Cells(0).Width.Value
OR
Dim intWidth As Integer = dvResults.HeaderStyle.Width.Value

Thanks,
Maurice
0
Comment
Question by:mauricerichard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 34128873
IMO, I'd use a GridView for the details because after databinding, you can do something like...

	Protected Sub gvRowDataBind(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
		If e.Row.RowType = DataControlRowType.DataRow Then
			If e.Row.DataItem("someDataColumn").ToString = "someVal" Then
				gv.HeaderRow.Cells(0).Width = Unit.Pixel(150)
			End If
		End If
	End Sub

Open in new window


Better to set the headrow cell width rather than multiple widths for each row, which can result in misbehaving HTML.  The DetailsView doesn't appear to have sufficient functionality for post-formatting (AFAIK),
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34131089
You can use DetailsView with OnDataBound event also.

Add the OnDataBound event to your DetailsView declaration.  

<asp:DetailsView ID="dv" runat="server" OnDataBound="dv_OnDataBound">

The Code Behind function will be called at runtime when you DataBind your DetailsView :
Protected Sub dv_OnDataBound(sender As Object, e As EventArgs)
      Dim dView As DetailsView = DirectCast(sender, DetailsView)
      dView.HeaderRow.Cells(0).Width = Unit.Pixel(100)
End Sub

0
 
LVL 1

Author Comment

by:mauricerichard
ID: 34136489
Hello Rouchie & BurnieP,

Thanks very much for the suggestions.  Rouchie, sadly replacing the detailsview with a gridview isn't really an option for me.  But I appreciate the idea, you might be right that the detailsview doesn't have the functionality ... cause I'm having no luck what-so-ever.  BurnieP, that's seems like it has potential.  Although what I'm trying to do is retrieve the runtime column width as opposed to assigning it.

For example, imagine a detailsview column at runtime:
If the longest text field in that column is: "Dog" - the column width would be very small
If the longest text field is " The quick brown fox jumped over the lazy dogs - It'll be much larger

So I need to be able to do something like:
Dim intColumnWidthAtRuntime as Integer = dView.HeaderRow.Cells(0).Width

Will that work?  Or how could we tweak it to work?

Thanks!
Moe
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 2000 total points
ID: 34136602
You can't do that from code-behind I'm afraid, because the width is set by the browser based on the cell contents, which then affects the whole column.  Therefore, you'd have to use JQuery or JavaScript to determine the width once the page finished loading and all the content was in there.
0
 
LVL 1

Author Closing Comment

by:mauricerichard
ID: 34148209
Looks like I won't be doing this from code-behind then.  Thanks for the help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question