Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:51:25+00:00 2026-05-27T04:51:25+00:00

i am trying to export the elements in gridview to excel spreadsheet. that grid

  • 0

i am trying to export the elements in gridview to excel spreadsheet. that grid hides several columns depending on the condition, here is some of the code

in the def of the button, this is the part where i have prob with exporting

    worksheet = workbook.Worksheets["Sheet1"];
    //string wname = tab.Text.Replace(" ", "_");
    //worksheet.Name = wname;
    range = worksheet.Cells["A1"];

    for (int i = 0; i < fr_chart_grid.Columns.Count; i++)//count = 7
    {
        if (fr_chart_grid.Columns[i].Visible == true)
            dtData.Columns.Add(fr_chart_grid.HeaderRow.Cells[i].Text);
    }


    //  add each of the data rows to the table
    foreach (GridViewRow row in fr_chart_grid.Rows)
    {
        DataRow drData;
        drData = dtData.NewRow();
        for (int i = 0; i < row.Cells.Count; i++)
        {
            if (fr_chart_grid.Columns[i].Visible == true)
                drData[i] = row.Cells[i].Text;
        }
        dtData.Rows.Add(drData);
    }

    range.CopyFromDataTable(dtData, SpreadsheetGear.Data.SetDataFlags.None);

and here is how my grid is defined

<asp:GridView ID="fr_chart_grid" runat="server" AutoGenerateColumns="False">
  <Columns>
     <asp:BoundField DataField="Dateval" HeaderText="Date" DataFormatString="{0:d}" HtmlEncode="false" />
     <asp:BoundField DataField="Col1" HeaderText="Data" DataFormatString="{0:f3}" />
     <asp:BoundField DataField="Col2" HeaderText=""  DataFormatString="{0:f3}"/>
     <asp:BoundField DataField="Col3" HeaderText=""  DataFormatString="{0:f3}"/>
     <asp:BoundField DataField="Col4" HeaderText=""  DataFormatString="{0:f3}"/>
     <asp:BoundField DataField="Col5" HeaderText=""  DataFormatString="{0:f3}"/>
     <asp:BoundField DataField="Col6" HeaderText=""  DataFormatString="{0:f3}"/>
   </Columns>
</asp:GridView>

my problem is that i have blank columns when i export the datafield, and sometimes wuts suppose to be in col 3 goes into col 4, with col 3 being blank. othertimes i have 5 rows and it tells me it cannot find the 5th row…it is frustrating….is there a way i could get rid of the blank columns? it’s random and it doesn’t show when i run the website, but it causes prob when i export to excel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:51:26+00:00Added an answer on May 27, 2026 at 4:51 am

    It looks like you have at least one issue… where you set the data for a cell in your new row, you are indexing i in both row.Cells and drData — They are out of sync when some columns are not visible.

    This line is trouble:

    drData[i] = row.Cells[i].Text; 
    

    drData may have fewer items than i due to non-visible columns. You’ll be overwritting data that is intended for other columns, or getting exceptions. You need to make sure you index drData with the index of the visible columns.

    Something like this should work:

    int j = 0;
    for (int i = 0; i < row.Cells.Count; i++)
    {
        if (fr_chart_grid.Columns[i].Visible == true)
        {
            drData[j] = row.Cells[i].Text;
            j++;
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying Export data to excel sheet from GridView but having this
I trying to export an HTML table named Table that is dynamically binded to
I am trying to export some prepared assets out of Expression Design 2 and
Am trying to export some portal pages with the theme.In the out put xml
I am trying to export some data from a SQL Express table to Access
I'm trying to export some tables generated with PHP from MySQL data. I'm trying
I am trying to export an excel file directory into an Oracle table as
I am trying to export an HTML table to excel but I cannot set
Im trying to export a .dmp of an oracle database but am getting the
I'm trying to export a Crystal Report to a text file, while preserving any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.