[Silverlight] 當麻許的Phone7筆記 - 8.輸入的鍵盤設定(InputScope)

2011/01/12

之前分享過一篇文章,是把資料輸出成 CSV檔案 - [Silverlight] 在Silverlight中輸出成 *.CSV 檔案

但是這一篇是根據一個迴圈,或是List<Data> 去做的,現在這一篇來分享根據DataGrid所顯示的資料產生CSV…

因為你可能輸出時候不要把某一欄的資料給輸出…

介紹一下配置..

blog-158


案例說明:dataGrid1會先Bind 一些資料進來,在btnToCSV時候會把 dataGrid1資料輸出成CSV
如果是按下 btnToCSV2  會先移除DataGrid第一欄測試看看輸出CSV是不是會沒有第一欄…

首先先建立一個 Extension Methods 當然你也可以不需要這樣做

DataGridExtendUtility.cs


using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ExportToCSVByOneCode
{
    public static class DataGridExtendUtility
    {
        /// <summary>
        /// 依照DataGrid內容取得每一列的資料
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static ICollection<DataGridRow> GetRows(this DataGrid grid)
        {
            List<DataGridRow> rows = new List<DataGridRow>();
            foreach (var rowItem in grid.ItemsSource)
            {
                grid.ScrollIntoView(rowItem, grid.Columns.Last());
                FrameworkElement fel = grid.Columns.Last().GetCellContent(rowItem);
                DataGridRow row = DataGridRow.GetRowContainingElement(fel.Parent as FrameworkElement);
                if (row != null) rows.Add(row);
            }
            return rows;
        }

        public static void ToCSV(this DataGrid grid)
        {

            var title = "";


            foreach (var c in grid.Columns)
            {
                title += "\t" + c.Header.ToString();
            }
            title = title.Remove(0, 1);


            string data = "" + title;
            data += "\r\n";

            foreach (DataGridRow rowItem in grid.GetRows())
            {

                foreach (var c in grid.Columns)
                {
                    var res = "";
                    try
                    {
                        DataGridCell cell = c.GetCellContent(rowItem.DataContext).Parent as DataGridCell;
                        res = (cell.Content as TextBlock).Text;
                    }
                    catch
                    {
                        res = "";
                    }
                    data += res + "\t";
                }
                data += "\r\n";
            }



            SaveFileDialog sfd = new SaveFileDialog()
            {
                DefaultExt = "csv",
                Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                FilterIndex = 1
            };
            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {
                    using (StreamWriter writer = new StreamWriter(stream, System.Text.UnicodeEncoding.Unicode))
                    {
                        writer.Write(data);
                        writer.Close();
                    }
                    stream.Close();
                }
            }
        }
    }
}

其中有一個 Extension Methods  叫做 ToCSV 讓 DataGrid 物件都有能力按照DataGrid內部資料輸出成CSV

這時候我們呼叫 輸出成CSV


private void btnToCSV_Click(object sender, RoutedEventArgs e)
{
          this.dataGrid1.ToCSV();
}


結果:
blog-159


再來就是測試我移除第一個欄位在輸出成csv
Code :

private void btnToCSV2_Click(object sender, RoutedEventArgs e)
{
    this.dataGrid1.Columns.RemoveAt(0);
    this.dataGrid1.ToCSV();
}

結果:
blog-160

移除完第一欄位後並且輸出…

有需要的可以自行修改成更符合自己想要用的…

Preview:




讚一下:

下載:


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.