How to get column name from gridview?

后端 未结 6 1936
南旧
南旧 2020-12-20 15:32

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...)

thanks.

6条回答
  •  余生分开走
    2020-12-20 16:08

    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.

    private static IDictionary GetGridViewFieldNames(object grid)
    {
        var names = new Dictionary();
        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;
    }
    

提交回复
热议问题