views:

1010

answers:

1

Yesterday i installed C# 3.5 and today when read topic here of "Accessing Google Spreadsheets with C# using Google Data API", and decided to try it out.

So i decided to write a example, that: 1) get data feed of first Worksheet of first Spreadsheet, that name contains string segment that user inserted 2) fill grid with data

Well i got to part, fill 2 dimensional array or lists of list with spreadsheet value data. I am stuck at finding a control to put data into, and dynamically generating it, as i don't know rows or columns of worksheet beforehand.

My intuition says, i could just change List> with Dataset, and start trying out different controls, that IntelliSense throws at me. I am aware that i should read a book first, but i was to impatient not try it out first.

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="[email protected]" />
        <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>

And 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);
        }
    }
}
A: 

I've used this from the 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" />

With some code behind to populate it:

private DataTable dataTable;

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

    dataGridView.DataContext = dataTable;
    ....
}
ChrisF
And it works :)
Margus
Great! I realise I've left the bit where you populate the data table - but I'm sure you've worked that out.
ChrisF