How to bind an List<List<String>> to the gridview?
I'm using the new Telerik Rad Control and I would to use the GridView included.
But my problem is I recover with a webservice a List<List开发者_如何学Python<String>>
object and I would like to show it into my Datagridview.
I try to make a Grid.ItemSource = e.result but nothing appears ;-(
What's the problem ?
Thanks a lot.
Narglix
It's a bit unclear how you're wanting to display the data, does "Each row will be a List" mean that you want it to be displayed in a hierarchal way like what's described here http://www.telerik.com/help/silverlight/gridview-basic-hierarchies.html?
In any case, you'll need to either take the approach suggested in the Telerik forum and create a datatable and bind to that, OR you create classes for the items returned in the lists and then create lists of those classes. Then bind to those lists.
Assuming you want to display it hierarchically:
For example if your List<List<String>>
is a list of teams and the inner lists are the team members. To Produce a result, where each team is a row in the main grid and you can "drill down" into the teams use the example below.
XAML
<telerikGrid:RadGridView x:Name="theGrid" AutoGenerateColumns="False">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding TeamName}" Header="Team" UniqueName="Team" />
</telerikGrid:RadGridView.Columns>
<telerikGrid:RadGridView.ChildTableDefinitions>
<telerikGrid:GridViewTableDefinition>
<telerikGrid:GridViewTableDefinition.Relation>
<telerikData:PropertyRelation ParentPropertyName="TeamMembers" />
</telerikGrid:GridViewTableDefinition.Relation>
</telerikGrid:GridViewTableDefinition>
</telerikGrid:RadGridView.ChildTableDefinitions>
</telerikGrid:RadGridView>
</Grid>
C# public partial class MainPage : UserControl {
public List<Team> TeamList { get; set; }
public MainPage() {
InitializeComponent();
//Property to bind to
TeamList = new List<Team>();
//Fake data returned from webservice
List<List<string>> eresults = new List<List<string>>();
List<string> innerList1 = new List<string> { "John", "Sarah", "Brad" };
List<string> innerList2 = new List<string> { "Dave", "Lucy", "Eva" };
eresults.Add(innerList1);
eresults.Add(innerList2);
int counter = 0;
foreach (List<string> innerList in eresults) {
Team theTeam = new Team();
theTeam.TeamName = string.Format("Team {0}", counter);
foreach (string s in innerList) {
TeamMember theMember = new TeamMember();
theMember.MemberName = s;
theTeam.TeamMembers.Add(theMember);
}
TeamList.Add(theTeam);
counter++;
}
theGrid.ItemsSource = TeamList;
}
}
public class Team
{
public List<TeamMember> TeamMembers { get; set; }
public String TeamName { get; set; }
public Team() {
TeamMembers = new List<TeamMember>();
}
}
public class TeamMember
{
public String MemberName { get; set; }
}
You can check my answer (and the attached application) here: http://www.telerik.com/community/forums/silverlight/gridview/how-to-bind-an-list-lt-list-lt-string-gt-gt-to-the-gridview.aspx
you have to put theGrid.ItemsSource in the loop
精彩评论