views:

3040

answers:

4

I have a row of data that I need to modify in a database, using a stored procedure. But in order to call that stored procedure, I need to know the name of the each column. How do I determine the name of the columns? (Hardcoding is not an option as we are talking a LOT of columns whose names may change).

EDIT: given the accepted answer, it looks like eviljack wanted the header text of the column and not the name of the bound field

+1  A: 

assuming you are using BoundField columns, you can get the Columns collection from the GridView and cast (from DataControlField) to BoundField, the get the DataField property

Steven A. Lowe
+2  A: 

To get the header text of the column you can use this:

string colText = grid.Columns[i].HeaderText;

Where i is the index of the column.

the header text and the database column name are not necessarily the same thing...
Steven A. Lowe
A: 
                foreach (TableCell objCell in e.Row.Cells)
                {
                    if (objCell is DataControlFieldHeaderCell)
                    {
                        string HEADERTEXT = objCell.Text;

                    }
                 }
salman
A: 
''' <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
ChrisS