How to get column name from gridview?

后端 未结 6 1925
南旧
南旧 2020-12-20 15:32

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.

6条回答
  •  猫巷女王i
    2020-12-20 15:49

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

提交回复
热议问题