Unable to bind specific rows to gridview control after selecting them from dataset

43 views Asked by At

I am assigning dataset to gridview datasource property in following manner:

if (subCategId == "20")
                    {
                        grdPHPortion.DataSource = _ds.Tables[0].AsEnumerable().Where(dr => dr.Field<int>("SubCategId") == 20).ToList();
                        grdPHPortion.DataBind();
                    }

But I am getting exception saying that:

A field or property with the name 'ItemName' was not found on the selected data source

While debugging I checked my dataset(_ds) in dataset visualiser and ItemName property is available in dataset.

The other syntax I tried is :

grdElecPortion.DataSource = _ds.Tables[0].Select("SubCategId='20'");

but then again I get the same exception. Please help in solving this exception.

EDIT:

Gridview markup is this:

<asp:GridView ID="grdPHPortion" runat="server" CssClass="" AutoGenerateColumns="false" Width="100%">
  <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  <Columns>
    <asp:TemplateField HeaderText="S. No.">
      <ItemTemplate>
        <%# ((GridViewRow)Container).RowIndex + 1%>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Checklist Item" DataField="ItemName" />
    <asp:BoundField HeaderText="Checklist ItemId" DataField="ItemId" Visible="false" />
    <asp:TemplateField HeaderText="Checklist Item Status">
      <ItemTemplate>
        <div class="col col-10">
          <label class="select">
                                                <asp:DropDownList ID="grdddlStatus" runat="server">
                                                    <asp:ListItem Text="--Select Status--" Value="-1"></asp:ListItem>
                                                    <asp:ListItem Text="Bad" Value="0"></asp:ListItem>
                                                    <asp:ListItem Text="Good" Value="1"></asp:ListItem>
                                                    <asp:ListItem Text="House fit for biding" Value="2"></asp:ListItem>
                                                    <asp:ListItem Text="House fit for biding after minor repair" Value="3"></asp:ListItem>
                                                    <asp:ListItem Text="House fit for biding after major repair" Value="4"></asp:ListItem>
                                                </asp:DropDownList>
                                            </label>
        </div>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Remarks" DataField="Remarks" />
    <asp:TemplateField HeaderText="Inspection Remarks">
      <ItemTemplate>
        <div class="col col-10">
          <label class="input">
                                                <i class="icon-append fa fa-terminal"></i>
                                                <asp:TextBox ID="txtInspRemks" runat="server" placeholder="Inspection Remarks"></asp:TextBox>
                                            </label>
        </div>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
  <EditRowStyle BackColor="#999999" />
  <FooterStyle BackColor="#D4E28D" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" />
  <HeaderStyle BackColor="#D4E28D" Font-Bold="false" ForeColor="Black" />
  <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#E9E7E2" />
  <SortedAscendingHeaderStyle BackColor="#506C8C" />
  <SortedDescendingCellStyle BackColor="#FFFDF8" />
  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

Basically I have 3 gridviews in my .aspx page and based on the value of "SubCategId" I have to show the related gridview, and yes, at a single point one or more than one gridview can be visible.

1

There are 1 answers

0
hafiya shah On

You can convert the dataset into list by traversing through the datarows and then try to bind the grid with the list.

            DataSet ds = new DataSet();
            ds = obj.getXmlData();// get the multiple table in dataset.

            Employee objEmp = new Employee ();// create the object of class Employee 
            List<Employee > empList = new List<Employee >();
            int table = Convert.ToInt32(ds.Tables.Count);// count the number of table in dataset
            for (int i = 1; i < table; i++)// set the table value in list one by one
            {
                foreach (DataRow dr in ds.Tables[i].Rows)
                {
                    empList.Add(new Employee { Title1 = Convert.ToString(dr["Title"]), Hosting1 = Convert.ToString(dr["Hosting"]), Startdate1 = Convert.ToString(dr["Startdate"]), ExpDate1 = Convert.ToString(dr["ExpDate"]) });
                }
            }
            dataGridView1.DataSource = empList;