3

Excelシートからグリッドビューにデータをインポートしました。その後、グリッドビューの列ヘッダーテキストをドロップダウンに入れたいのですが、それらにアクセスできません.plzを助けてください:-(;

これは私のコードですが、動作しません:

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

この*行では、列がないことがわかります (column.count = 0)

4

3 に答える 3

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; }
    }
于 2012-07-23T11:46:49.240 に答える
1

次のコードを使用できます

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();
于 2012-07-23T11:56:09.867 に答える
0

gridView1_CellValueChangedグリッドのイベントにアクセスしてからアクセスするe.Column.FieldName.ToString()

于 2012-07-23T12:33:15.883 に答える