根据用户选择显示gridview列

时间:2016-08-23 06:14:17

标签: c# gridview

我有一个Gridview,想根据用户选择动态制作标题。

如何创建从用户中选择的标头。

1 个答案:

答案 0 :(得分:0)

DataTable将帮助您实现目标。请按照以下代码

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();
}