Search code examples
csvdevexpressexport-to-csvxtragrid

on button click export to csv using devexpress xtragrid


I m using devxtra grid control and devexpress xtra bar.What i want to do is

1) I have a button on the bar which should select all the rows in the grid and then export to csv. I m using Itemclick event and in that i m using gridView1.SelectAll() but in the output nothing is being selected.

**************************Designer*******************************

           // barButtonSelectAll

        // 
        this.barButtonSelectAll.Caption = "Select All";
        this.barButtonSelectAll.Id = 1;
        //this.barButtonSelectAll.Glyph = true;
        this.barButtonSelectAll.Width = 50;
   this.barButtonSelectAll.LargeGlyph=global::Binder.Resources.close_16;
        this.barButtonSelectAll.Name = "barButtonSelectAll";
        this.barButtonSelectAll.ItemClick+=newDevExpress.XtraBars.ItemClickEventHandler(this.barButtonSelectAll_ItemClick);
        //  

            // gridControl1
        // 
        this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.gridControl1.Location = new System.Drawing.Point(0, 0);
        this.gridControl1.MainView = this.gridView1;
        this.gridControl1.MenuManager = this.barCopyResult;
        this.gridControl1.Name = "gridControl1";
        this.gridControl1.Size = new System.Drawing.Size(922, 441);
        this.gridControl1.TabIndex = 4;
        this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
        this.gridView1});
        // 
        // gridView1
        // 
        this.gridView1.GridControl = this.gridControl1;
        this.gridView1.Name = "gridView1";
        this.gridView1.OptionsView.ShowGroupPanel = false;
        // 

**************************Code*******************************

 private void barButtonSelectAll_ItemClick (object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        //StreamWriter sw = new StreamWriter("D:\\gridview.csv");
        //for (int i = 0; i < gridView1.Columns.Count; i++)
        //{
    //sw.Write(gridView1.Columns[i].AppearanceHeader.TextOptions.ToString());
        //  if (i != gridView1.Columns.Count)
        //  {
        //      sw.Write(",");
        //  }
        //  sw.Write(sw.NewLine);
        //  foreach( GridViewRow dr in gridView1.RowCount)
        //}
        gridView1.SelectAll();
        for (int i = 0; i < gridView1.RowCount; i++)
        {
            //gridView1.GetDetailView(1,1);
            gridControl1.ExportToCsv("D:\\csv docs");
        }
        //MessageBox.Show("Hola");
    }

Solution

  • At first, check the GridView.OptionsSelection.MultiSelect property because If this property is set to false, the GridView.SelectAll method does nothing.

    At second, to export all the data displayed by the GridControl's view to the specified file in CSV format you should not select something in this view. And moreover, you should not do call this method in cycle for each row.
    Just use the following code:

    void barButtonExport_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
        gridView1.ExportToCsv(@"D:\csv docs\1.csv");
    }
    

    Remarks:
    The ExportToCsv method indirectly calls the DevExpress.XtraPrinting.PrintingSystemBase.ExportToCsv method of the XtraPrinting Library. If this library is not available, the method does nothing.

    For general information on exporting data in the GridControl refer to Export Overview.