List <Tuple <T1, T2 >> as DataSource for GridView

I would like to add List<Tuple<T1,T2>> as a DataSource for my GridView .

"Then do it!"

Yes, this is not a problem, the problem is accessing the values ​​inside the GridView.

Here is my code:

 List<Tuple<Group, string>> userGroups = Util.PrepareGroups((string[][])Session["userGroups"]); gridGroups.DataSource = userGroups; gridGroups.DataBind(); 

Throws an exception in the DataBind , telling me that Item1.Name does not exist, speaking of this, here is my markup:

 <asp:GridView runat="server" ID="gridGroups" CssClass="grid gridGroups" AutoGenerateColumns="false"> <Columns> <asp:BoundField meta:resourcekey="gridGroupsName" DataField="Item1.Name" /> <asp:BoundField meta:resourcekey="gridGroupsFunction" DataField="Item2" /> </Columns> </asp:GridView> 

Needless to say, Item1 is a group, and Name is a string -Property.
He (yes, he, my IDE is called Bob) obviously does not find Item1.Name, is there any way to avoid it . ?

Thanks,

Dennis

+4
source share
1 answer

Subscribe to the RowDataBound event in your code that will fire for each related row, and you can use the arguments provided for the event to access the current data item - it will be easier for you to get your values ​​in such a way as to try to combine with the data binding syntax and etc.

(ps - remember to check the rowtype property in the event handler, which is easy to do - you will get unexpected results / errors if you try to access the data object for the header / footer line! the link above has some code that demonstrates this)

+3
source

All Articles