在C#3.5中填充网格

时间:2009-05-11 13:11:58

标签: c# wpf .net-3.5 datagrid google-docs

昨天我安装了C#3.5,今天在阅读“使用Google Data API使用C#访问Google Spreadsheets”这一主题时,我决定尝试一下。

所以我决定写一个例子,那个: 1)获取第一个电子表格的第一个工作表的数据馈送,该名称包含用户插入的字符串段 2)用数据填充网格

我得分开,用电子表格值数据填充二维数组或列表列表。我坚持找到一个控件来放入数据并动态生成它,因为我事先不知道工作表的行或列。

我的直觉说,我可以改变List>使用数据集,并开始尝试不同的控件,IntelliSense抛出我。我知道我应该先读一本书,但我不耐烦而不先试试。

的Xaml

<DockPanel Background="LightSteelBlue">

    <!-- Standard input -->
    <DockPanel DockPanel.Dock="Top" Margin="5">
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Username:</TextBlock>
        <TextBox x:Name="username" Width="120" Padding="5,0,5,0" Text="username@gmail.com" />
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Password:</TextBlock>
        <TextBox x:Name="password" Width="120" Padding="5,0,5,0" Text="password" />
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Workbook:</TextBlock>
        <TextBox x:Name="workbookname" Width="120" Padding="5,0,5,0" Text="name" />
        <Button x:Name="getData" Margin="5,0,0,0" Padding="5,0,5,0" Content="Get _Data" Click="getData_Click" />
    </DockPanel>

    <!-- Grid display -->
    <ListBox Name="lb2"/>

</DockPanel>

和c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

namespace getGoogleDocsSpreadsheetData
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private bool RunSample(
            String g_username,
            String g_password,
            String g_spreadsheetname
        ){
            SpreadsheetsService service = new SpreadsheetsService("Margus-stackoverflow_example-1");
            service.setUserCredentials(g_username, g_password);
            CellFeed cfeed = null;

            try {
                //get CellFeed of first Worksheet of first Spreadsheet, that contains _spreadsheetname
                cfeed = ((CellFeed)service.Query(new CellQuery(
                    ((WorksheetEntry)(service.Query(new WorksheetQuery(
                        ((SpreadsheetEntry)(
                            from fe in service.Query(new SpreadsheetQuery()).Entries
                            where fe.Title.Text.Contains(g_spreadsheetname)
                            select fe
                        ).First()).Links.FindService(
                            GDataSpreadsheetsNameTable.WorksheetRel,
                            null
                        ).HRef.ToString())).Entries.First())
                    ).Links.FindService(
                        GDataSpreadsheetsNameTable.CellRel,
                        null
                    ).HRef.ToString()))
                );

                /* get data to array
                string[,] values = new string
                    [(from sr in cfeed.Entries select ((CellEntry)sr).Cell.Row).Max()+1
                    ,(from sr in cfeed.Entries select ((CellEntry)sr).Cell.Column).Max()+1];

                foreach (CellEntry curCell in cfeed.Entries)
                    values[curCell.Cell.Row,curCell.Cell.Column] = curCell.Cell.Value;
                 */

                /* get data to list of list of string */
                List<List<string>> lists = new List<List<string>>();

                for (int i = 0; i < 1+ (
                    from sr in cfeed.Entries 
                    select ((CellEntry)sr).Cell.Row).Max(); i++
                ){
                    List<string> x = new List<string>();
                    for (int j = 0; j < 1+ (
                        from sr in cfeed.Entries 
                        select ((CellEntry)sr).Cell.Column).Max(); j++
                    )
                    x.Add(default(string));

                    lists.Add(x);
                }

                foreach (CellEntry curCell in cfeed.Entries)
                    (lists[(int)curCell.Cell.Row])[(int)curCell.Cell.Column] = curCell.Cell.Value;

                //fill datagrid
                this.lb2.ItemsSource = lists;              
            } catch (Exception e){
                System.Console.WriteLine("I exeeded failing!\n" + e.StackTrace);
                return false;
            }

            return true;
        }

        private void getData_Click(object sender, RoutedEventArgs e)
        {
            RunSample(
                this.username.Text,
                this.password.Text,
                this.workbookname.Text);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在WPFToolkit中使用了这个:

<my:DataGrid Margin="0,29,0,0" Name="dataGridView" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" AlternatingRowBackground="AliceBlue" ItemsSource="{Binding Path=.}" CanUserResizeRows="False" ClipboardCopyMode="IncludeHeader" IsReadOnly="True" IsTabStop="True" AutoGeneratedColumns="dataGridView_AutoGeneratedColumns" />

用一些代码填充它:

private DataTable dataTable;

public Constructor()
{
    ....
    this.dataTable = new DataTable();

    dataGridView.DataContext = dataTable;
    ....
}