【问题标题】:Retrieve data from database and store in string and bind all the strings to gridview从数据库中检索数据并存储在字符串中并将所有字符串绑定到gridview
【发布时间】:2013-08-21 03:57:21
【问题描述】:

我正在使用存储过程将数据从数据库中提取到 gridview 中。我不想将列名指定为gridview中boundfield的datafeild属性。我想在后面的代码中将所有列提取到单独的字符串中,并在数据字段中指定字符串名称。

提前致谢

【问题讨论】:

  • 那么您想使用存储过程返回的列名在代码隐藏中动态创建BoundFields?

标签: asp.net sql-server-2008 c#-4.0


【解决方案1】:

您可以循环访问从数据库中的存储过程返回的DataTable,如下所示:

// Iterate through the columns of the DataTable to set the BoundFields dynamically
foreach (DataColumn theDataColumn in theDataTable.Columns)
{
    // Instantiate new BoundField
    BoundField theBoundField = new BoundField();

    // Set the DataField value
    theBoundField.DataField = theDataColumn.ColumnName;

    // Set the HeaderText value
    theBoundField.HeaderText = theDataColumn.ColumnName;

    // Add BoundField to the GridView
    GridView1.Columns.Add(theBoundField);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多