Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Why I like the ASP.NET GridView's model better than the SL DataGrid
5 replies. Latest Post by IanW on November 27, 2008.
(0)
IanW
Member
8 points
16 Posts
11-21-2008 2:44 PM |
If I have a column like this:
Column 1
----------------------------------
5000
$35
....
I can easily format the first cell (5000) as a generic number and then have the first cell, row 2 have a currency format. In the databinding event of a grid in ASP.NET, it's possible to intercept the value and format particular cells.
In SL, there's no real way to get to cells in the datagrid events. The templating features don't appear to allow the formatting of individual cells since the cells are not exposed by the Row object on the event args. Everything is the same format in a column. The templating stuff for the SL datagrid doesn't appear to offer any solution to the need for formatting one row differently from another row.
:) Hopefully I'm wrong and someone can point me in the right direction.
preishuber
Contributor
3570 points
655 Posts
11-23-2008 8:59 AM |
i am not shure if i understand it right (perhaps you have a screenshot)
yes asp.net 2.0 is better then Silverlight. But there is a lot possible with datagrid in silverlight, eg you can use converter
http://weblogs.asp.net/hpreishuber/archive/2008/11/18/rownumber-in-silverlight-datagrid-or-listbox.aspx
You have also templates to show more than one field in one line
11-24-2008 12:55 AM |
Thanks for the reply.
I want this:
COLUMN 1
-------------------------------------
I get this
$5000
Yes, you can use converters and you can even pass in a parameter for a converter (like a row number; but not a cell index).
The problem though is that the DataGrid does not support obtaining access to cells, so in the example I gave, there's no way to determine at runtime that cell 0 is being loaded and then conditionally format its contents (from currency to whole number using the parameter in the converter methods). (Templates don't address the problem I'm describing. I really really really want to be wrong on this point ;) ). SL is a step back in many ways from ASP.NET development since it doesn't have nearly as rich an object model as the humble html grid. Even the DataGrid from 1.1 supports the scenario I'm describing.
Thus, in SL, I believe I should have access to the cells collection in a DataGridRow that is processed during the PrepareRow event. I get this in the analogous events with a 1.1 DataGrid.
11-24-2008 1:39 AM |
Here's a link containing a screen shot. The first cell shouldn't be formatted as a currency; the cells beneath it are correct.
http://www.flickr.com/photos/sultanofsuede/3055577610/
Jonathan...
All-Star
24646 points
2,418 Posts
11-27-2008 1:04 AM |
Hi IanW,
The easier way is to make your column1 bind to an object. It contains several properties. Then we can use Converter to convert it to different types. For example,
<data:DataGridTextColumn x:Name="index" Header="Index" Visibility="Visible" Width="50" Binding="{Binding CVTObject,Converter={StaticResource rowNumberConverter}}"/> //CVTObject is the instance of ConvertObject.
public class ConvertObject { private float currency; public float Currency { get { return currency;} set { this.currency = value; } } private string converter; public string Converter { get { return converter; } set { converter = value; } } }
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ConvertObject cvtobj = value as ConvertObject; float convertedValue = 0; if (cvtobj != null) { if (cvtobj.Converter == "$") { //format your currency as what you desire } else { convertedValue = cvtobj.Currency; } //counter++; } return convertedValue; }
Best regards,
Jonathan
11-27-2008 1:31 AM |
Thank you very much Jonathan!
I'll give this a whirl, but it looks like a very sound strategy to me and this will I believe solve my problem.
While I don't have a need at the moment, I'm not sure if this would be able to handle the formatting of a cell since it only deals with the actual object being bound. So, if I needed to format a cell with red text because it was a negative value, I'm stuck because there's no way to access the cell.
Thanks again!
Ian