2013-08-21 59 views
-1

我正在使用存儲過程將數據從數據庫提取到gridview中。我不想在gridview中將列名指定爲boundfield的datafeild屬性。我想在後面的代碼中將所有列提取到單獨的字符串中,並在數據字段中指定字符串名稱。從數據庫中檢索數據並存儲在字符串中並將所有字符串綁定到gridview

在此先感謝

+0

所以要動態地創建在代碼隱​​藏的'BoundField's使用從存儲過程返回的列名稱? –

回答

0

可以遍歷數據庫從存儲過程返回,這樣你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); 
} 
相關問題