【问题标题】:How to use DataReader with GridView in ASP.Net如何在 ASP.Net 中使用 DataReader 和 GridView
【发布时间】:2019-03-05 11:22:00
【问题描述】:

我正在尝试在 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】:

    您应该使用Eval。但是您发布的错误与使用DataReader

    无关
    <asp:TemplateField>
        <ItemTemplate>
    
            <%# Eval("Data1") %>
    
        </ItemTemplate>
    </asp:TemplateField>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      相关资源
      最近更新 更多