如何在ASP.Net中使用DataView与GridView

问题描述 投票:0回答:1

我正在尝试在GridView中使用DataReader 而不是使用DataAdapter。

但我在aspx中有这个错误:

不可调用的成员'IDataItemContainer.DataItem'不能像方法一样使用

我在这行中有错误:

<%# Container.DataItem("Data1")%>

我有这个代码:

SqlConnection baglan=new SqlConnection("connectionstring text");
if (connection.State== ConnectionState.Closed)
{
    baglan.Open();
}
SqlCommand cmd = new SqlCommand("Select Data1 from Table1",baglan); 
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>

    <asp:TemplateField>
        <ItemTemplate>                            

            <%# Container.DataItem("Data1")%>

        </ItemTemplate>

        <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
    </asp:TemplateField>
    </Columns>
</GridView>

我怎样才能解决这个问题 ?谢谢

asp.net gridview datareader
1个回答
0
投票

你应该使用Eval。但是你发布的错误与使用DataReader无关

<asp:TemplateField>
    <ItemTemplate>

        <%# Eval("Data1") %>

    </ItemTemplate>
</asp:TemplateField>
© www.soinside.com 2019 - 2024. All rights reserved.