获取UltraWinGrid的可见列 [英] get visible columns of UltraWinGrid

查看:155
本文介绍了获取UltraWinGrid的可见列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的Windows应用程序中有一个UltraWinGrid。

我的应用程序用户可以修改WinGrid(例如选择他想要在网格中看到的列并且还改变它们的顺序)然后当他按下打印按钮时,当前具有当前列和序列和数据的WinGrid应该作为DataTable类型中的参数发送到类。



但问题是DataSource中的列和序列数与屏幕上显示的当前WinGrid不匹配。

Hi all,
I have a UltraWinGrid in my windows application.
my application user can modify the WinGrid (for example select columns that he want to see in grid and also change sequence of them) and then when he press ''Print'' button the current WinGrid with current columns and sequence and data should send to a class as a parameter in DataTable type.

but the problem is that the sequence and number of columns in the DataSource does not match to current WinGrid that is shown on screen.

推荐答案

已故回复....但如果有人正在寻找这个可能会有所帮助。





Late response.... But might help if some one is looking for this.


/// <summary>
     /// Exports all the Visible Columns from an UltraGrid to Excel
     /// If you are looking at above C# 3.5 then refer Microsoft.CSharp Assembly in your project references
     /// </summary>
     /// <param name="Grid">UltraGrid</param>
     public static void ExportToExcel(this UltraGrid Grid)
     {
         if (Grid == null || Grid.Rows.Count == 0) return;

         var xlApp = new Excel.Application();
         try
         {
             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

             List<String> Columns = new List<string>();

             ///Setting the Current Culture...Can remove this
             Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

             Excel.Workbooks workbooks = xlApp.Workbooks;
             Excel.Range range;
             Excel.Workbook workbook = workbooks.Add();
             Excel.Worksheet worksheet = workbook.Worksheets[1];

             long totalCount = Grid.Rows.Count;
             long rowRead = 0;

             UltraGridColumn column = Grid.DisplayLayout.Bands[0].Columns[0];

             /// Get the next visible column by passing in VisibleRelation.Next.
             column = column.GetRelatedVisibleColumn(VisibleRelation.First);

             while (null != column)
             {
                 Columns.Add(column.Key);

                 /// Get the next visible column by passing in VisibleRelation.Next.
                 column = column.GetRelatedVisibleColumn(VisibleRelation.Next);
             }


             ///Create Columns Object and Select those Columns only
             for (var i = 0; i < Columns.Count; i++)
             {
                 UltraGridColumn Column = Grid.DisplayLayout.Bands[0].Columns[Columns[i]];

                 worksheet.Cells[1, i + 1] = Column.Header.Caption;
                 range = (Excel.Range)worksheet.Cells[1, i + 1];
                 range.Interior.ColorIndex = 20;
                 range.Font.Bold = true;
             }

             ///Now fill in the Rows to Excel
             for (var r = 0; r < Grid.Rows.Count; r++)
             {
                 for (var i = 0; i < Columns.Count; i++)
                 {
                     UltraGridColumn Column = Grid.DisplayLayout.Bands[0].Columns[Columns[i]];

                     if (Column.Format != null)
                     {
                         ///If you have defined Specific formats and would like them to be part of the excel
                         ///then you can customize as below
                         if (Column.Format.Equals("##,###") || Column.Format.Equals("#,##"))
                         {
                             worksheet.Cells[1, i + 1].EntireColumn.NumberFormat = "#,###;(#,###);-";
                             worksheet.Cells[r + 2, i + 1] = Grid.Rows[r].Cells[Columns[i]].Value;
                         }
                         else if (Column.Format.StartsWith("##,###.##") || Column.Format.Equals("#,##.##"))
                         {
                             worksheet.Cells[1, i + 1].EntireColumn.NumberFormat = "#,##0.00;(#,##0.00);-";
                             worksheet.Cells[r + 2, i + 1] = Grid.Rows[r].Cells[Columns[i]].Value;
                         }
                         else if (Column.Format.StartsWith("MM/dd/yyyy"))
                         {
                             worksheet.Cells[1, i + 1].EntireColumn.NumberFormat = "MM/dd/yyyy";
                             worksheet.Cells[r + 2, i + 1] = Grid.Rows[r].Cells[Columns[i]].Value;
                         }
                         else
                         {
                             worksheet.Cells[r + 2, i + 1] = Grid.Rows[r].Cells[Columns[i]].Value;
                         }

                     }
                     else
                     {
                         worksheet.Cells[r + 2, i + 1] = Grid.Rows[r].Cells[Columns[i]].Value.ToString();
                     }
                 }

                 rowRead++;
             }

             Microsoft.Office.Interop.Excel.Range columns = worksheet.UsedRange.Columns;
             columns.AutoFit();

             ///Display the Excel Component to User
             xlApp.Visible = true;
         }
         catch (Exception ex)
         {
             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
             System.Windows.Forms.MessageBox.Show("Exception occured while exporting rows to Excel. Exception Details : " + ex.Message.ToString(), "Excel Export", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
         }
         finally
         {
             ///Force Clean up the Com Component to remove any traces
             GC.Collect();
             GC.WaitForPendingFinalizers();
             foreach (Excel.Workbook wb in xlApp.Workbooks)
             {
                 foreach (Excel.Worksheet ws in wb.Worksheets)
                 {
                     Marshal.FinalReleaseComObject(ws);
                 }
                 wb.Close(false, Type.Missing, Type.Missing);

                 Marshal.FinalReleaseComObject(wb);
             }
             xlApp.Quit();
             Marshal.FinalReleaseComObject(xlApp);

             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
         }
     }


这篇关于获取UltraWinGrid的可见列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆