2012-03-07 80 views

回答

11

你可以這樣說:

gv.HeaderRow.Cells[i].Text 

或者這樣:

gv.Rows[0].Cells[i].Text 

行[0]應該是你的標題行。

+4

請不要忘記標記爲:D – JoRouss 2012-03-07 18:15:26

+2

37分鐘後,並且沒有標記答案。也許這個通知會提醒他們。 – Khan 2012-03-07 18:54:09

+0

嗨我試過了,我只能得到這個數字,我需要得到列名 – 2012-03-08 14:10:35

2

簡單

GridView1.Rows[0].Cells[0].Text; 

,如果你想從每一行試試這個的所有單元格值

foreach (GridViewRow r in GridView1.Rows) 
    { 
     string s = r.Cells[0].Text; 
     string y = r.Cells[1].Text; 
    } 

更新:

試試這個

foreach (TableCell Tc in GridView1.HeaderRow.Cells) 
    { 
     //if you are not getting value than find childcontrol of TabelCell. 
     string sssb = Tc.Text; 
     foreach (Control ctl in Tc.Controls) 
     { 

      //Child controls 
      Label lb = ctl as Label; 
      string s = lb.Text; 
     } 
    } 
+0

嗨我試過了,我只獲得數字,我需要獲得列名 – 2012-03-08 14:10:26

+0

可能是你的列控單元在HeaderRow.try的childcontrol上,我的更新code.and讓我知道會發生什麼? – Jigs 2012-03-08 16:21:17

+0

我得到的對象引用未設置爲對象字符串的實例s = lb.text – 2012-03-08 17:18:42

0

這不是問題的答案。這只是答案,如果你從不改變gridview中的標題文本。問題的提問者想要通過索引獲取數據庫字段名稱。

我已經看到了一些「答案」,它只能提供一個gridview,或兩者都不回答被問的問題的標題文字的選定行中的文本......

您可能問爲什麼?如果您打算對系統中的更新進行審計,並且想要比較舊值和新值,並且只在更改時更新並且想要指示哪個字段已更新,那麼如何在索引處顯示數據庫表字段名稱循環。

例如:

Protected Sub gv_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) 
    Dim intValCount As Integer = Integer.Parse(e.NewValues.Count) 
    If intValCount > 0 Then 
     Dim i As Integer = 0 
     For i = 0 To intValCount - 1 
      If Not e.OldValues(i).Equals(e.NewValues(i)) Then 
       ' values have changed, audit the change 
       Sys.Audits.General(intID, "the_database_table", <the table field by index(i)>, e.OldValues(i).ToString, e.NewValues(i).ToString) 
      End If 
      i += 1 
     Next 
    End If 
End Sub 

所以,問題是你如何通過指數通過代碼獲取數據庫表字段名稱的後面?我也一直在尋找這個,所見到的所有「答案」都不是我認爲我們有些人真正想要的答案。我在這裏看到的所有提到的方法都不是對數據庫表字段名稱的防彈引用。

3
//// Header column names 
int gridViewCellCount = yourGridView.Rows[0].Cells.Count; 
// string array to hold grid view column names. 
string[] columnNames = new string[gridViewCellCount]; 

for (int i = 0; i < gridViewCellCount; i++) 
{ 
    columnNames[i] = ((System.Web.UI.WebControls.DataControlFieldCell)(yourGridView.Rows[0].Cells[i])).ContainingField.HeaderText; 
} 
1

重溫這個老問題....有可能通過這種類型的代碼獲取綁定到GridView的數據的字段名。這使得colnum和字段名稱的字典。

private static IDictionary<int, string> GetGridViewFieldNames(object grid) 
{ 
    var names = new Dictionary<int, string>(); 
    var view = grid as GridView; 
    if (view != null) { 
     for (var i = 0; i < view.Columns.Count; i++) { 
      var field = view.Columns[i] as BoundField; 
      if (field != null) { 
       names.Add(i, field.DataField); 
      } 
     } 
    } 
    return names; 
} 
+0

只有在這裏需要一個刺字段名稱而不是列標籤文本。 – 2016-08-26 21:14:52

0

它很簡單:在這裏,我閱讀每個標題單元格的gridview標題文本,並將它們作爲新列添加到數據表中。

DataTable dt = new DataTable(); 
foreach(DataControlFieldHeaderCell column in yourGridview.HeaderRow.Cells) 
{ 
    dt.Columns.Add(column.Text.Trim().Replace(" ", ""));       
} 

//Make sure you do all this after yourGridview.DataBind(); 
//If you do not want to bind data first simply bind an empty list like so: 
/* yourGridview.DataSource = new List<string>(); 
    yourGridview.DataBind(); */