ストアドプロシージャを使用して、データベースで変更する必要のあるデータの行があります。ただし、そのストアドプロシージャを呼び出すには、各列の名前を知る必要があります。列の名前を確認するにはどうすればよいですか?(名前が変更される可能性のある多くの列について話しているため、ハードコーディングはオプションではありません)。
編集:受け入れられた答えを考えると、eviljackはバインドされたフィールドの名前ではなく列のヘッダーテキストを望んでいたようです
列のヘッダー テキストを取得するには、これを使用できます。
string colText = grid.Columns[i].HeaderText;
ここで、i は列のインデックスです。
BoundField 列を使用していると仮定すると、GridView から Columns コレクションを取得し、(DataControlField から) BoundField にキャストして、DataField プロパティを取得できます。
''' <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
List<string> ColName = new List<string>();
foreach(DataColumn c in gridview.Columns)
{
ColName.Add(c);
}
foreach (TableCell objCell in e.Row.Cells)
{
if (objCell is DataControlFieldHeaderCell)
{
string HEADERTEXT = objCell.Text;
}
}