How to get column name from gridview?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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

Method 4

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

Method 5

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.

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…

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 name at 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.

Method 6

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

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(); */


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x