Janaki
Janaki

Reputation: 47

Binding a column within a generic List to GridView

I tried reading all the articles and tried the solutions as well did not work for me.

One of the solutions i tried

Binding Generic List Array to GridView.

The new project file that I created using your tweeked sample code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePointProject2
{
public class abc
{
      public string customername { get; set; }
    public List<def> DEF { get; set; }
}

public class def
{
    public string materialcode { get; set; }
}

}

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace SharePointProject2.testing
{
public partial class testingUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
            List<abc> test1 = new List<abc>();
            List<def> test2 = new List<def>();
            test2.Add(new def() { materialcode = "something" });
            test1.Add(new abc() { customername = "anything", DEF = test2 });
            GridView1.DataSource = test1;
            GridView1.DataBind();

    }


}

}

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="testingUserControl.ascx.cs" Inherits="SharePointProject2.testing.testingUserControl" %>  

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Charge Code">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"     Text='<%# ((List<def>)(Eval("DEF")))[0].materialcode %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>

When I compile the code above, this is the error messsage

The type or namespace name 'def' could not be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 5604

Answers (2)

Kelsey
Kelsey

Reputation: 47726

EDIT: Sorry I had misread a lot of your question. Your example does not compile at all so I made a quick test app example to illustrate the solution:

public class def   {   public string materialcode{get;set;}   }
public class abc
{
    public string customername { get; set; }
    public List<def> DEF { get; set; }
}

List<abc> test1 = new List<abc>();
List<def> test2 = new List<def>();
test2.Add(new def() { materialcode = "something" });
test1.Add(new abc() { customername = "anything", DEF = test2 });

grdTest.DataSource = test1;
grdTest.DataBind();

I have tested the above and it works. Leave a comment if you require any clarification.

EDIT: The following should get you up and running, and is in my opinion, a cleaner way of implementing custom binding and allows you to debug the issue much easier.

Change your Label in your ItemTemplate to the following:

<asp:Label ID="lblchargecode" runat="server"
    Text='' OnDataBinding="lblchargecode_DataBinding"></asp:Label>

Add the DataBinding method to your codebehind:

protected void lblchargecode_DataBinding(object sender, System.EventArgs e)
{
    Label lbl = (Label)sender;
    lbl.Text = ((List<def>)(Eval("DEF")))[0].materialcode;
}

Also make sure to check your includes. You need to make sure that your code knows about the def class so include a using statement where required or fully qualify it.

Upvotes: 1

Harsh Baid
Harsh Baid

Reputation: 7249

change it to

<asp:TemplateField HeaderText="Charge Code">  
<ItemTemplate>

<asp:Label ID="lblchargecode" runat="server" Text='<%# ((List<def>) Eval("DEF"))[0].materialcode %>'></asp:Label>
</ItemTemplate>  
</asp:TemplateField>  

Eval returns object you have to cast it to original type then use it .

Upvotes: 0

Related Questions