Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need help on exporting only visible DataGridView columns to excel, I have this code for hiding columns in DataGridView.
C#
this.dg1.Columns[0].Visible = false;

And then I have button click event for exporting to excel.
C#
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();

// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

// see the excel sheet behind the program
app.Visible = true;

// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;


// changing the name of active sheet
worksheet.Name = "PIN korisnici";

// storing header part in Excel
for (int i = 1; i < dg1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dg1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dg1.Rows.Count - 1; i++)
{
for (int j = 0; j < dg1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
}
}


But I want to export only visible columns, while I get all of them, anyone, help on this. :)
Posted
Updated 25-Nov-11 0:47am
v2

You should do a check first to see if the row is visible first like this

C#
// storing Each row and column value to excel sheet
            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Visible)
                {
                    for (int j = 0; j < dg1.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
                    }
                }
            }


Hope this helps
 
Share this answer
 
Comments
shonezi 25-Nov-11 7:07am    
I tried but its not working
Wayne Gaylard 25-Nov-11 7:11am    
Did you get an exception or what?
shonezi 25-Nov-11 7:20am    
no, nothing, I do an export but those columns that I made invisible
are exported also
Wayne Gaylard 25-Nov-11 7:36am    
Put a breakpoint in the code where you check if the row is visible and check if it return true always.
Try this code

for (iC = 0; iC <= colsTotal; iC++)
{
if (dgvstocklist.Columns[iC].Visible)
{
_with1.Cells[1, iC + 1].Value = dgvstocklist.Columns[iC].HeaderText;
}

}
for (I = 0; I <= rowsTotal - 1; I++)
{ for (j = 0; j <= colsTotal; j++)
{
if (dgvstocklist.Columns[j].Visible)
{
_with1.Cells[I + 2, j + 1].value = dgvstocklist.Rows[I].Cells[j].Value;
}

}
}

And put the Columns (whatever you want to hide) at last in the select command,and make those columns invisible.
 
Share this answer
 
You make one manual dataSet for invisible columns as no.(number) and invisible column's Name.
loop to grids visible false columns and add those in dataset.

Now export all grid data to Excel.
After completion of excel writing,do formating to excel-do loop to above dataset
and do those excels columns as visible false.

if it helps then accept else reply.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900