2008-10-13 61 views
1

我有一行数据需要使用存储过程在数据库中修改。但为了调用该存储过程,我需要知道每列的名称。我如何确定列的名称? (因为我们正在谈论很多名字可能会改变的列,硬编码不是一种选择)。以编程方式确定GridView控件的列名称?

编辑:给予接受的答案,它看起来像eviljack希望列的标题文本,而不是绑定字段

+0

做你想做的列标题文本或将列绑定到数据库字段的名称? – 2008-10-13 19:42:38

回答

3

要得到列的标题文本您可以使用此:

串colText = grid.Columns [I] .HeaderText;

其中i是该列的索引。

+0

标题文本和数据库列名不一定是相同的东西... – 2008-10-13 19:42:08

2

的名称假设你使用绑定列列,你可以从GridView的列集合和流延(从的DataControlField)来绑定列,在得到数据字段属性

0
   foreach (TableCell objCell in e.Row.Cells) 
       { 
        if (objCell is DataControlFieldHeaderCell) 
        { 
         string HEADERTEXT = objCell.Text; 

        } 
       } 
0
''' <summary> 
''' Requires that the 'AccessibleHeaderText' property is set on the column in the aspx 
''' </summary> 
Private Function GetCellByName(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs, ByVal colName As String) As System.Web.UI.WebControls.TableCell 

    Dim result As TableCell = Nothing 

    If e.Row.RowType = DataControlRowType.DataRow Then 
     Dim grid As GridView = e.Row.NamingContainer 
     Dim index As Integer = 0 

     For i As Integer = 0 To grid.Columns.Count - 1 
      If String.Compare(grid.Columns(i).AccessibleHeaderText, colName, True) = 0 Then 
       index = i 
       Exit For 
      End If 
     Next 

     If index <= e.Row.Cells.Count Then 
      result = e.Row.Cells(index) 
     End If 
    End If 

    Return result 

End Function 
0
List<string> ColName = new List<string>(); 
foreach(DataColumn c in gridview.Columns) 
{ 
    ColName.Add(c); 
} 
+1

你可以添加一些解释吗? – Undo 2013-06-24 14:19:59

相关问题