Search code examples
c#asp.netexportexport-to-excel

HeaderRow Cell test is blank


There are few things that I am doing before exporting the grid to excel. I will try to explain those in a bit. The problem that I am running into that the

Grid.HeaderRow.Cells[1].Text is "".  

Apparently this has something to do with the SortExpression. Does anyone know if SortExpression is used, the Column Headers are blank. Any solutions.

Here is what I am doing (Not that what I am doing matters)

  1. Getting the data from the data Source
  2. Modifying the data (adding quantities, modifying price, etc)
  3. Getting the columns that the user needs to see Displaying the data (binding the grid with the data from 2 and columns from 3)
  4. Exporting this data to excel.

All columns have a SortExpression attached. When I try to get the header row text, it returns blank.

This is something I found link. Seems like this user also has the same problem as I have. Please read the reply at the botton

Please help. Thanks.


Solution

  • Ha! Answering my own question!

    You have to do:

    for(int i=0; i<grid.Columns.Count; i++)
    {
        var headertext = grid.Columns[i].HeaderText;
        dt.Columns.Add(new DataColumn(headertext));
    }
    

    and not cell.