C# 如何从gridview获取列名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9606720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:07:01  来源:igfitidea点击:

How to get column name from gridview?

c#asp.netsql

提问by Inzi Irina

I would like to know how can I get a column name from a gridview? by its number not by name. like : Name|Age|Birthday: ( so name=0 , age=1 etc...)

我想知道如何从 gridview 中获取列名?按编号而不是按名称。喜欢:姓名|年龄|生日:(所以姓名=0,年龄=1等...)

thanks.

谢谢。

采纳答案by JoRouss

You can get it like this :

你可以这样得到它:

gv.HeaderRow.Cells[i].Text

Or like this :

或者像这样:

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

Rows[0] should be your header row.

Rows[0] 应该是你的标题行。

回答by Jignesh Rajput

simply

简单地

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

try this if you want to all the cells value from each of the rows

如果你想要每一行的所有单元格值,试试这个

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

update:

更新:

try this

尝试这个

  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;
        }
    }

回答by user2321282

This is not the answer to the question. It is only the answer if you never change the header text in the gridview. The asker of the question wanted to get the database field name by index.

这不是问题的答案。只有当您从不更改 gridview 中的标题文本时,这才是答案。问题的提问者想通过索引获取数据库字段名称。

I've seen a number of "answers" which only provide the text in the selected row of a gridview, or the header text which both do not answer the question that was asked...

我见过一些“答案”,它们只提供 gridview 的选定行中的文本,或者都没有回答被问到的问题的标题文本......

You may ask why? If you were going to do audits of updates in a system and wanted to compare old values and new values and only update if they change and want to indicate which field was updated how do you show the database table field nameat the index of the loop.

你可能会问为什么?如果您要对系统中的更新进行审计,并且想要比较旧值和新值,并且仅在它们发生变化时才进行更新,并且想要指示哪个字段已更新,您如何在循环的索引处显示数据库表字段名称.

For instance:

例如:

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

So the questions is how do you get the database table field name by index via code behind? I too have been searching for this and all the "answers" that I've seen are not the answers I think some of us are really looking for. All the mentioned methods I've seen here are not bullet proof references to a database tables field name.

所以问题是如何通过代码背后的索引获取数据库表字段名称?我也一直在寻找这个,我看到的所有“答案”都不是我认为我们中的一些人真正在寻找的答案。我在这里看到的所有提到的方法都不是对数据库表字段名称的防弹引用。

回答by user2862491

//// 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;
}

回答by O. Jones

Revisiting this old question.... it's possible to get the field names of the data bound to a GridView with this kind of code. This makes a dictionary of colnum and field name.

重温这个老问题......可以使用这种代码获取绑定到 GridView 的数据的字段名称。这使得一个列和字段名称的字典。

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;
}

回答by Trevor

Its easy: Here i read the gridview header text for each header cell and add them as new columns to a data table.

很简单:在这里,我读取每个标题单元格的 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(); */