0

I get the following error at the time of DataBinding() for the gridview. Eventhough the "siteName" field is defined as part of the structure "StructSelectedSiteList", the DataBinder(...) in ASP.net gridview definition does not recognize the field. All the fields and the structure are defined as public.

I had referred to previous posts, but it did not help. DataBinding does not contain a property

Appreciate your response. Thanks!

Here's the code. StructSelectedSiteList - structure which is populated with values. this is the data source.

[Serializable]
    public struct StructSelectedSiteList
    {
        public string siteName;
        public int taskId;
    };

In code below, I populate the structure.

 var lstSites = new List<StructSelectedSiteList>();

 LOOP1:

    // populate the struct for selected sites
    StructSelectedSiteList structSelectedSiteList;
    structSelectedSiteList.siteName = lblSiteName.Text;
    structSelectedSiteList.taskId = taskId;

    lstSites.Add(structSelectedSiteList);

Now the gridview is bound with populated structure.

gvSelectedSites.DataSource = lstSites;
gvSelectedSites.DataBind();   <<----- Error occurs here

The ASP.net gridview definition is-

                                <asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True"
                                CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI"
                                Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center"
                                EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>                                        
                                    <asp:TemplateField HeaderText="siteName">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "siteName")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Task" Visible="false">
                                        <ItemTemplate>
                                            <asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "taskId")%>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                               </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>
Community
  • 1
  • 1

1 Answers1

0

After testing I found out that I was accessing 'siteName' as structure field, but I need to access it as a property. Hence, after converting all the fields of the structure into properties, I am able to proceed without the exception. Thanks to solutions "DataBinding: 'index+NewsItem' does not contain a property with the name 'Link'", but property exists (Not a Typo) and 'myObject' does not contain a property with the name 'ID' (Not a typo)

Community
  • 1
  • 1