3

i imported my data from Excel sheet to grid view.after that i want to put the column header text of my grid view into drop down,but i can't access them help me plz :-( ;

this is my code but it dose not work :

List<string> lst = new List<string>();
* for (int i = 0; i < dg_excel.Columns.Count; i++)
{
    lst.Add(dg_excel.Columns[i].HeaderText);
}
ddl_prd_count.DataSource;
ddl_prd_count.DataBind();

for this * line it says to me there are no columns (column.count = 0)

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
H.Ghassami
  • 1,012
  • 2
  • 21
  • 42

3 Answers3

1

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; }
    }
Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
1

you can use following code

List<string> lst = new List<string>();

for (int i = 0; i < dg_excel.HeaderRow.Cells.Count; i++)
{

    lst.Add(dg_excel.HeaderRow.Cells[i].Text);

}

ddl_prd_count.DataSource=lst ;

ddl_prd_count.DataBind();
Pabuc
  • 5,528
  • 7
  • 37
  • 52
shrestha2lt8
  • 305
  • 1
  • 9
0

Access gridView1_CellValueChanged event of your grid and then accesse.Column.FieldName.ToString()

user1528573
  • 95
  • 1
  • 2
  • 9