Search code examples
c#devexpressxtragrid

DevExpress.XtraGrid.Views.Grid.GridView get rows


I have DevExpress.XtraGrid.Views.Grid.GridView gridView; there is also a pattern of Excel - shablon.xlt. I want when you click on "go" to save the template data from gridView.

private void button2_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook ObjWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet ObjWorkSheet;
            ObjWorkBook = ObjExcel.Workbooks.Open("D:\\shablon.xlt");
            ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBook.Sheets[1];
            ObjWorkSheet.Cells[3, 1] = "hz1";
            ObjWorkSheet.Cells[3, 2] = "hz2";
            ObjWorkSheet.Cells[3, 3] = "dfdsf";
            ObjExcel.Visible = true;ObjExcel.UserControl = true;
        }

I need to ObjWorkSheet.Cells [3, 1] = "hz1"; insert data from a similar cell gridView. e.g. ObjWorkSheet.Cells [3, 1] = gridView.GetRow (1); but such a mechanism in gridviev I have not found


Solution

  • If you want to export the data in GridView to Excel, GridView already provides an option

    gridView1.ExportToXls("export.xls");
    

    If at all you want to do it manually, you can do it using GetRowCellValue method

    var firstCellValue = gridView.GetRowCellValue(0,"ColumnName");