tags:

views:

723

answers:

2

I have a report page that can display two different reports. So I have a GridView with no columns on my aspx page. I am adding 4 BoundFields to the GridView in the button click event handler. The first column is the one I need to set the width of for one of the reports (the code I'm using to add this column is below).

gvReport.Columns.Clear();  
BoundField bf1 = new BoundField();  
...  
if (ddReportType.SelectedValue == "full") {  
bf1.HeaderText = "Facility";  
bf1.DataField = "Facility";  
bf1.ItemStyle.Wrap = true;  
bf1.ItemStyle.Width = 150;  
bf1.Visible = true;  
gvReport.Columns.Add(bf1);  
...

The problem is there is one row that has a SHA512 hash in this column. Since there is no space in the middle of it, the gridview won't wrap it (I think that's what is happening, anyway)! So I thought I'd catch this column in the OnRowDataBound event and add a space in the middle of the hash so it will wrap, but I can't figure out how to reference the BoundField. There's no ID property. Does anyone have a suggestion? Either on how to reference the BoundField, or another way to get this to display nicely?

I had the columns in the aspx file originally and tried using:
gvReport.Columns[0].ItemStyle.Width = 150;
gvReport.Columns[0].ItemStyle.Wrap = true;

but that didn't work either. This is very frustrating!

A: 

So we went with Plan C. I never did figure out how to reference the BoundField. We got around that problem by creating two GridViews in the markup and just changing which one is visible. To solve the problem of the text that won't wrap, we're catching it in the OnRowDataBound event handler and checking the length of the text. We just insert a space in the middle, and voila! It wraps!

Theresa
A: 

The BoundField is a wrong thing. It is more like a definition. Later, when binding or creating rows, you are dealing with rows, cells, and data items.

You could do something like this:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  string value = e.Row.Cells[1].Text;
  if (!string.IsNullOrEmpty(value))
      e.Row.Cells[1].Text = value.Insert(value.Length / 2, " ");
 }
}

where Cell[1] is the column containing long values. In the same place, you could use e.Row.DataItem to get actual data, but then you'd need to know its type.

You could create a template field and bind to an expression or use its child's data bound event to do the same.

Also, if you were using a DataSet, you could set up calculated field and bind to it. Anyway...

Ruslan
We used TemplateFields and ItemTemplates. Since we created two GridViews, we were able to bind the columns from the Oracle ref_cursors in the markup. Our split code was something like your example, but we knew exactly where we wanted to split it since it was a fixed-length hash.
Theresa