Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net c中将哈希集中的数据绑定到ItemTemplate_C#_Asp.net_Gridview_Hashtable_Itemtemplate - Fatal编程技术网

C# 如何在asp.net c中将哈希集中的数据绑定到ItemTemplate

C# 如何在asp.net c中将哈希集中的数据绑定到ItemTemplate,c#,asp.net,gridview,hashtable,itemtemplate,C#,Asp.net,Gridview,Hashtable,Itemtemplate,我在GridView中创建了项模板 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" onrowdatabound="GridView1_RowD

我在GridView中创建了项模板

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" onrowdatabound="GridView1_RowDataBound" >
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Enabled='<%# Eval("id") %>' 
                    Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
现在我的问题是,我将id存储在一个散列集中,因为我有一些重复的id,我只想显示唯一的id

var id = new HashSet<String>();
id.Add("1");
id.Add("1");
id.Add("2");
id.Add("3");
id.Add("4");
id.Add("5");
Gridview1.DataSource=id;
Gridview1.DataBind();
我认为Eval方法有问题,我不认为它是从Hashset获取值

有人能解释一下如何将Hashste中的数据绑定到Eval吗

另外,如果我不使用ItemTemplate,它也可以工作,我的意思是我可以直接从Hashset值填充GridView。但是,我尝试使用项模板创建嵌套的gridview


如果您有任何问题,请告诉我。

仅因为您命名了变量id,基础类型不包含属性id

您可以使用匿名类型作为数据源:

但是,您试图设置从Id启用的布尔属性。这似乎不正确


请注意,我已将哈希集的名称从id更改为ids,因为它包含多个值

您正在对集合使用Eval。是的,因为我认为它对集合有效。我还可以使用什么?您好,我得到的指定强制转换无效。你能用这种方式来转换哈希集吗?@John:你有没有注意到你正试图用你的id来设置bool属性?!对否则我怎么设置呢?@John:我想你应该改为设置文本属性。明白了。那太差劲了。我想现在可以了。谢谢你,蒂姆。祝你有美好的一天。
var ids = new HashSet<String>();
ids.Add("1");
ids.Add("1");
ids.Add("2");
ids.Add("3");
ids.Add("4");
ids.Add("5");
Gridview1.DataSource = ids.Select(id => new { id }).ToList();
Gridview1.DataBind();