How to get column name from gridview? How to get column name from gridview? asp.net asp.net

How to get column name from gridview?


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.


//// Header column namesint 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;}


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