Check answer on another SO thread Why column count is 0 for GridView to know that why are you not able to access the columns from your grid's Columns collection.
In my opinion, if you want to use auto generated columns then follow this ASP.net forum link - Customizing Auto Generated Columns (GridView) and SO thread How to hide columns in an ASP.NET GridView with auto-generated columns?.
e.g.
have an idea from this code.. i have not checked the proper syntax etc in it.
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (IsFillCombo)
{
//fill your list here.
for (int i = 0; i < datasourcetable.Columns.Count; i++)
{
lst.Add(e.Row.Cells[i].Text);
}
IsFillCombo = false;
}
}
}
// Another simplest way to implement this as below: From Question Text
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Abc> listofAbc = new List<Abc>();
for (int i = 0; i < 10; i++)
{
listofAbc.Add(new Abc
{
ID = i + 1,
Name = "Abc" + (i + 1).ToString()
});
}
GridView1.DataSource = listofAbc;
GridView1.DataBind();
List<string> lst = new List<string>();
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
lst.Add(GridView1.HeaderRow.Cells[i].Text);
System.Diagnostics.Debug.WriteLine(GridView1.HeaderRow.Cells[i].Text);
}
}
}
}
public class Abc
{
public int ID { get; set; }
public string Name { get; set; }
}