【问题标题】:How to show records in AspxGridView using dynamic databind?如何使用动态数据绑定在 AspxGridView 中显示记录?
【发布时间】:2016-12-19 09:57:06
【问题描述】:

我想用 AspxGridView 进行动态数据绑定。如果我设置AutoGenerateColumn = true 它显示选定列的列名,但不显示记录。如果我将 false 设置为自动生成列,则页码数量会增加但不显示记录。

Asp.net 代码

<dx:ASPxGridView ID="ASPxGridView1" runat="server">
    <SettingsPager PageSize="50">
    </SettingsPager>
    <Settings ShowFilterRow="True" ShowGroupPanel="True" />
    <SettingsCommandButton>
        <ShowAdaptiveDetailButton ButtonType="Image"></ShowAdaptiveDetailButton>
        <HideAdaptiveDetailButton ButtonType="Image"></HideAdaptiveDetailButton>
    </SettingsCommandButton>
    <SettingsDataSecurity AllowDelete="False" AllowEdit="False" AllowInsert="False" />
    <SettingsSearchPanel Visible="True" />
</dx:ASPxGridView>

cs 文件

protected void btnSearch_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["HQMatajerConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select * from [HQMatajer].[dbo].[TransactionEntry] where itemid='74876'";
        con.Open();
        SqlDataReader rd = cmd.ExecuteReader();

        //ASPxGridView1.Columns.Clear();
        ASPxGridView1.AutoGenerateColumns = true;
        ASPxGridView1.DataSource = rd;
        ASPxGridView1.DataBind();
    }
}

输出

如果我设置为 false,则以下图像是输出

【问题讨论】:

  • 你需要使用DataSet或DataTable而不是DataReader

标签: c# asp.net data-binding devexpress aspxgridview


【解决方案1】:

SqlDataReader 是快速获取记录的一个很好的解决方案,但您不能按原样使用它来填充 GridView 对象的 DataSource。您应该将它与 DataSet 或 DataTable 结合使用以实现您的目标。 这两个类都有 Load 属性,以便从 SQLDataReader 对象中获取结果。

SqlDataReader rd = cmd.ExecuteReader();
DataTable dt= new DataTable();
dt.Load(rd);
ASPxGridView1.DataSource = dt;

也可以看看https://msdn.microsoft.com/en-us/library/system.data.datatable.load(v=vs.110).aspx

【讨论】:

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