我需要使用存储过程在数据库中修改一行数据。但是为了调用该存储过程,我需要知道每列的名称。如何确定列的名称? (硬编码不是一个选项,因为我们正在谈论很多可能改变其名称的列。)
编辑:鉴于接受的答案,看起来像恶魔杰克想要列的标题文本而不是绑定字段的名称
答案 0 :(得分:3)
要获取列的标题文本,您可以使用:
string colText = grid.Columns [i] .HeaderText;
其中i是列的索引。
答案 1 :(得分:2)
假设您正在使用BoundField列,您可以从GridView获取Columns集合并将其从(从DataControlField)转换为BoundField,获取DataField属性
答案 2 :(得分:0)
foreach (TableCell objCell in e.Row.Cells)
{
if (objCell is DataControlFieldHeaderCell)
{
string HEADERTEXT = objCell.Text;
}
}
答案 3 :(得分: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
答案 4 :(得分:0)
List<string> ColName = new List<string>();
foreach(DataColumn c in gridview.Columns)
{
ColName.Add(c);
}