0

I have 2 classes:

public class clsShipper 
{
    public string ShipperName { get; set; }
}

and

public class clsJobInfo 
{
    public long JobID { get; set; }

    public clsShipper oShipper = new clsShipper();
}

My GridView is as follows

<asp:GridView ID="dgvJobCostList" runat="server" CellPadding="4" ForeColor="#333333" style="font-family:Verdana;font-size:10px;" AutoGenerateColumns="False" OnSelectedIndexChanged="dgvJobCostList_SelectedIndexChanged">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkRowCheck" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lblJobID" runat="server" Text='<%# Bind("JobID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Shipper">
            <ItemTemplate>
            <asp:Label ID="lblShipperName" runat="server" Text='<%# Bind("ShipperName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>  

I am trying to bind the ShipperName property of Shipper Class through the JobInfo Class object. I tried below

protected void btnSearch_Click(object sender, EventArgs e)
{
    if (oOutputJobInfo.Length < 1)
    {
        lblMessage.Text = "Error : No Data Found";
        lblMessage.Visible = true;
        return;
    }
    else
    {
        dgvJobCostList.DataSource = oOutputJobInfo;
        dgvJobCostList.DataBind();
        dgvJobCostList.Visible = true;
    }
}

But gives below error

DataBinding: 'Nucleus.BOL.clsJobInfo' does not contain a property with the name 'ShipperName'.

4
  • also tried this <asp:Label ID="lblShipperName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Shipper.ShipperName") %>'></asp:Label> instead of <asp:Label ID="lblShipperName" runat="server" Text='<%# Bind("ShipperName") %>'></asp:Label> but no luck Commented May 21, 2012 at 11:28
  • You should not name classes with prefix cls! Commented May 21, 2012 at 11:40
  • 1
    @abatishchev, yeh ! But this is the convention my office follows. Not able to change it. :( Commented May 21, 2012 at 11:50
  • @abatishchev - Right, because it breaks the compiler. The developer can prefix with foo, bar, boo, cls, my or whatever as long as they can read it and it's the standard they have chosen for their team. Commented May 21, 2012 at 11:59

1 Answer 1

2

Import the namespace like so:

<%@ Import namespace="MyNamespace.ToclsJobInfo" %>

And you might need the namespace to clsShipper if different than clsJobInfo

Then you can cast/unbox clsJobInfo and use the property oShipper

<asp:TemplateField HeaderText="Shipper">    
  <ItemTemplate>    
    <asp:Label ID="lblShipperName" runat="server" Text='<%# ((clsJobInfo)Container.DataItem).oShipper.ShipperName %>' />    
  </ItemTemplate>  
</asp:TemplateField>
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this <asp:Label ID="lblShipperName" runat="server" Text='<%# DataBinder.Eval(((clsShipper)Container.DataItem).ShipperName) %>'></asp:Label> but it gives this compile time error Error 5 The type or namespace name 'clsShipper' could not be found (are you missing a using directive or an assembly reference?)
Also, I don't think you need DataBinder.Eval when unboxing.
@V.P.Verma - sorry, I had the class names backwards. I've updated my answer.
finally this solved the problem <asp:Label ID="lblShipperName" runat="server" Text='<%# ((clsJobInfo)Container.DataItem).oShipper.ShipperName %>'></asp:Label> Thanks to all of you :D The StackOverFlow guys rocks !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.