回答
你可以這樣說:
gv.HeaderRow.Cells[i].Text
或者這樣:
gv.Rows[0].Cells[i].Text
行[0]應該是你的標題行。
簡單
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;
}
}
嗨我試過了,我只獲得數字,我需要獲得列名 – 2012-03-08 14:10:26
可能是你的列控單元在HeaderRow.try的childcontrol上,我的更新code.and讓我知道會發生什麼? – Jigs 2012-03-08 16:21:17
我得到的對象引用未設置爲對象字符串的實例s = lb.text – 2012-03-08 17:18:42
這不是問題的答案。這只是答案,如果你從不改變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
所以,問題是你如何通過指數通過代碼獲取數據庫表字段名稱的後面?我也一直在尋找這個,所見到的所有「答案」都不是我認爲我們有些人真正想要的答案。我在這裏看到的所有提到的方法都不是對數據庫表字段名稱的防彈引用。
//// 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;
}
重溫這個老問題....有可能通過這種類型的代碼獲取綁定到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;
}
只有在這裏需要一個刺字段名稱而不是列標籤文本。 – 2016-08-26 21:14:52
它很簡單:在這裏,我閱讀每個標題單元格的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(); */
- 1. 使用Linq,我如何從GridView獲取列名列表?從DataTable?
- 2. 如何從GridView獲取列寬?
- 3. 如何從模型LINQ獲取列名?
- 4. 如何獲取隊列名稱NServiceBus從
- 5. 從列號獲取列名?
- 6. 如何從gridview獲取副本?
- 7. 如何從GridView(C#)獲取選定值
- 8. 如何從gridView獲取圖像(位圖)
- 9. 如何從GridView獲取位置
- 10. 如何從gridview獲取單元格值?
- 11. 如何從GridView獲取滾動位置?
- 12. 從GridView獲取RepositoryLookupEdit
- 13. 從GridView獲取值
- 14. 如何從gridview中獲取標題文本而不固定列
- 15. 從AbstractTableModel獲取列名稱
- 16. 從表中獲取列名
- 17. 如何從gridview中獲取複選框的值和子gridview
- 18. 如何從gridview中提取行gridview c#
- 19. SSRS - 如何按列名獲取列值
- 20. 如何隱藏gridview列按列名
- 21. 如何從SqlDataReader獲取列名的表名
- 22. 從GridView獲取數據集?
- 23. 從gridview中獲取數據
- 24. 從ASP.NET GridView獲取DataRow
- 25. c#從gridview獲取值
- 26. 從gridview獲取多行DataKeys
- 27. 如何從ColumnHeaderContextMenu獲取列?
- 28. 如何從GridView讀取
- 29. 如何從preg_match獲取命名捕獲?
- 30. 如何從SharePoint列表中獲取列名稱?
請不要忘記標記爲:D – JoRouss 2012-03-07 18:15:26
37分鐘後,並且沒有標記答案。也許這個通知會提醒他們。 – Khan 2012-03-07 18:54:09
嗨我試過了,我只能得到這個數字,我需要得到列名 – 2012-03-08 14:10:35