asp.net 从dataset中选择特定行后,无法将它们绑定到gridview控件

0g0grzrc  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(120)

我将dataset分配给gridview datasource属性,方式如下:

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

字符串
但我得到的例外是:
在所选数据源上找不到名为“ItemName”的字段或属性
在调试时,我在dataset visualiser中检查了我的dataset(_ds),并且ItemName属性在dataset中可用。
我尝试的其他语法是:

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


但是我又得到了同样的异常。请帮助解决这个异常。

编辑:

Gridview标记是这样的:

<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>


基本上我有3个gridviews在我的.aspx页面和基于“SubCategId”的值,我必须显示相关的gridview,是的,在一个单一的点一个或多个gridview可以是可见的。

vnjpjtjt

vnjpjtjt1#

您可以通过遍历datarowsdataset转换为列表,然后尝试用列表 * bind * 网格。

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;

字符串

相关问题