I make button_click event for export data from gridview in c# winforms to excel spreadsheet as following
private void copyAlltoClipboard()
{
myGridView.SelectAll();
DataObject dataObj = myGridView.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void btnExportExcel_Click(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
Clipboard.SetDataObject("");
}
the problem that i repeat them so much in every view how i can make this two mmthods public and just call them and there is only one variable which is myGridView Regards