按钮单击使用devexpress xtragrid导出到csv

时间:2014-05-18 16:58:17

标签: csv devexpress export-to-csv xtragrid

我使用devxtra网格控件和devexpress xtra bar。我想做的是

1)我在栏上有一个按钮,应该选择网格中的所有行,然后导出到csv。 我正在使用Itemclick事件,并且使用gridView1.SelectAll(),但在输出中没有选择任何内容。

**************************设计***************** **************

           // 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;
        // 

**************************代码***************** **************

 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");
    }

1 个答案:

答案 0 :(得分:0)

首先,检查GridView.OptionsSelection.MultiSelect属性,因为如果此属性设置为false,则GridView.SelectAll方法不执行任何操作

第二,要将GridControl视图显示的所有数据导出为CSV格式的指定文件,您不应在此视图中选择某些内容。而且,你不应该为每一行循环调用这个方法 只需使用以下代码:

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

说明:
ExportToCsv方法间接调用XtraPrinting Library的DevExpress.XtraPrinting.PrintingSystemBase.ExportToCsv方法。如果此库不可用,则该方法不执行任何操作。

有关在GridControl中导出数据的一般信息,请参阅Export Overview