如何根据数据添加标题列在GridView的数据库获取 [英] How to add header columns based on data fetch from the database in gridview

查看:103
本文介绍了如何根据数据添加标题列在GridView的数据库获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件,并希望进行动态的基础上,一些SQL查询头像...

I have a GridView and want to make headers dynamically based on the some SQL query like...

select question from quiz where quizid is 123.

此查询将返回 * 基于问题数 quizid

This query will return * number of questions based on the quizid.

如何创建头与一个已经从数据库中选择数据?

How to create headers with the data that's been selected from database?

推荐答案

您可以使用数据表,以帮助这一点。

You can use DataTable to help with this.

我不知道你使用的数据库管理哪些技术,但我用 LINQ to SQL的。而下面的是我的样本:

I don't know which technologies you used for database management, but I used LinQ to SQL. And the following is my sample:

DataClassesDataContext db = new DataClassesDataContext();

protected DataTable GetDataSource() 
{
    DataTable dt = new DataTable();

    var questions = db.ExecuteQuery<string>("select question from quiz where quizid is 123").ToList();

    // Header implementation
    int count = 0;
    foreach (var question in questions)
    {
        DataColumn dc = new DataColumn(question);
        dt.Columns.Add(dc);
        count++;
    }

    // Rows implementation here
    DataRow row = dt.NewRow();
    ...
    dt.Rows.Add(row);

    return dt;
}


protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = GetDataSource();
    GridView1.DataBind();
}

这是我的aspx code:

And here is my aspx code:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

这篇关于如何根据数据添加标题列在GridView的数据库获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆