I want to export the columns I pulled into the
datagridview
with
Checkbox
to Word. However, it also exports the hidden columns to Word that I have not checked with the
checkbox
es and that do not appear in the
datagridview
. I want to export only selected columns as shown in the image. What is the solution?
What I have tried:
public void Export_Data_To_Word(DataGridView DGV, string filename)
{
if (DGV.Rows.Count != 0)
{
int RowCount = DGV.Rows.Count;
int ColumnCount = DGV.Columns.Count;
Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
int r = 0;
for (int c = 0; c <= ColumnCount - 1; c++)
{
for (r = 0; r <= RowCount - 1; r++)
{
DataArray[r, c] = DGV.Rows[r].Cells[c].Value;
}
}
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
dynamic oRange = oDoc.Content.Application.Selection.Range;
string oTemp = "";
for (r = 0; r <= RowCount - 1; r++)
{
for (int c = 0; c <= ColumnCount - 1; c++)
{
oTemp = oTemp + DataArray[r, c] + "\t";
}
}
oRange.Text = oTemp;
object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
oRange.ConvertToTable(ref Separator, ref RowCount,
ref ColumnCount,
Type.Missing, Type.Missing,
ref ApplyBorders,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, ref AutoFit,
ref AutoFitBehavior, Type.Missing);
oRange.Select();
oDoc.Application.Selection.Tables[1].Select();
oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.InsertRowsAbove(1);
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name =
"Tahoma";
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;
for (int c = 0; c <= ColumnCount - 1; c++)
{
oDoc.Application.Selection.Tables[1].Cell
(1, c + 1).Range.Text = DGV.Columns[c].HeaderText;
}
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.Cells.VerticalAlignment =
Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
foreach (Word.Section section in
oDoc.Application.ActiveDocument.Sections)
{
Word.Range headerRange =
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange,
Word.WdFieldType.wdFieldPage);
headerRange.Text = "your header text";
headerRange.Font.Size = 16;
headerRange.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
oDoc.SaveAs2(filename);
}
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection
(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =
siniflisteleri25.accdb;
Jet OLEDB:Database Password = Fatih2541; Mode = ReadWrite"))
{
string query = "SELECT tcno,ono,isim,soyisim,cinsiyet,sinifi,
dtarihi,atel,btel from ogrencibilgileri25 where
sinifi='" + ComboBox1.Text + " '";
OleDbCommand command = new OleDbCommand(query, conn);
conn.Open();
var adapter = new OleDbDataAdapter(command);
var table = new System.Data.DataTable();
adapter.Fill(table);
dataGridView3.Columns["tcno"].Visible = tcnochk.Checked;
dataGridView3.Columns["dtarihi"].Visible = dogchk.Checked;
dataGridView3.Columns["dtarihi"].Visible = dogchk.Checked;
dataGridView3.Columns["atel"].Visible = atelchk.Checked;
dataGridView3.Columns["btel"].Visible = btelchk.Checked;
dataGridView3.DataSource = table;
conn.Close();
}
}