Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

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

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
mauricerichard
Asked:
mauricerichard
  • 2
  • 2
1 Solution
 
RouchieCommented:
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
 
BurniePCommented:
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
 
mauricerichardAuthor Commented:
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
 
RouchieCommented:
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
 
mauricerichardAuthor Commented:
Looks like I won't be doing this from code-behind then.  Thanks for the help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now