Как связать ответ API API листов Google в datagridview, используя c # и wpf - PullRequest
0 голосов
/ 08 июля 2019

У меня есть IList<IList<Object>>, в котором хранятся данные, извлеченные из таблиц Google api v4, и я пытаюсь заполнить эти данные в виде таблицы данных.Я могу вручную вставить эти данные в представление, но при зацикливании представление всегда заканчивается пустым.

public static IList<IList<Object>> Get(UserCredential credential, string spreadsheetId, string range)
{
    var service = new SheetsService(new BaseClientService.Initializer()
    { HttpClientInitializer = credential, ApplicationName = ApplicationName, });

    SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);

    ValueRange response = request.Execute();
    return response.Values;
}
private void populate_Table(object sender, RoutedEventArgs e)
{   // This function on_click populates the datagrid named JumpTable
    this.JumpTable.ItemsSource = null; // Clears the current datagrid before getting new data
    // Pulls in the 2d table from the client sheet
    IList<IList<Object>> client_sheet = Get(SetCredentials(), "$spreadsheetIdPlaceholder", "Client!A2:H");
    // Dictionary to set the properties that will be populated in the datagrid
    Dictionary<string, object> entries = new Dictionary<string, object>();

    /** [Status] [Non Functional]
    for (int i = 0; i < 5; i++) // Using 5 as the count for testing
    {
        entries.Add("Tracker", new[] { new { // DataGrid To Add To
            A = string.Format("{0}", client_sheet[i][0]), // Column [A] To Populate
            B = string.Format("{0}", client_sheet[i][1]),
            C = string.Format("{0}", client_sheet[i][2]),
            D = string.Format("{0}", client_sheet[i][3])
        } });
    } 
    **/

    /** [Status] [Functional]
    entries.Add("Tracker", new[] { new { // DataGrid To Add To
        A = string.Format("{0}", client_sheet[0][0]), // Column [A] To Populate
        B = string.Format("{0}", client_sheet[0][1]),
        C = string.Format("{0}", client_sheet[0][2]),
        D = string.Format("{0}", client_sheet[0][3])
    } });
    **/

    this.JumpTable.DataContext = entries; // Binds the entries to the datagrid
    /** Alternative binding solution
        * this.JumpTable.ItemsSource = entries;
    **/
}
            <DataGrid Grid.Row="3" 
                      ItemsSource="{Binding [Tracker]}"
                      ScrollViewer.CanContentScroll="True" 
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Auto"
                      Name="JumpTable" 
                      CanUserSortColumns="True" 
                      CanUserAddRows="False" 
                      AutoGenerateColumns="False"
                      materialDesign:DataGridAssist.CellPadding="13 8 8 8" 
                      materialDesign:DataGridAssist.ColumnHeaderPadding="8" 
                      Margin="0,10,0,0" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Tracker ID" IsReadOnly="True" Binding="{Binding A}" Width="275"/>
                    <DataGridTextColumn Header="Request ID" IsReadOnly="True" Binding="{Binding B}" Width="80"/>
                    <DataGridTextColumn Header="Req Requested Date" IsReadOnly="True" Binding="{Binding C}" Width="100"/>
                    <DataGridTextColumn Header="Requested Start Date" IsReadOnly="True" Binding="{Binding D}" Width="100"/>
                </DataGrid.Columns>
            </DataGrid>

1 Ответ

0 голосов
/ 08 июля 2019

Решил, переключив тип из словаря в Список объектов

private void populate_Table(object sender, RoutedEventArgs e)
{   // This function on_click populates the datagrid named JumpTable
    // this.JumpTable.ItemsSource = null; // Clears the current datagrid before getting new data
    // Pulls in the 2d table from the client sheet
    IList<IList<Object>> client_sheet = Get(SetCredentials(), "$spreadsheetIdPlaceholder", "Client!A2:AY");
    // List to set the properties that will be populated in the datagrid
    List<Object> entries = new List<Object>();


    // [Status] [Functional]
    for (int i = 0; i < 30; i++) // Using 30 as the count for testing
    {
        entries.Add(new
        {
            A = client_sheet[i][0], B = client_sheet[i][1], C = client_sheet[i][2], D = client_sheet[i][3],
            E = client_sheet[i][4], F = client_sheet[i][5], G = client_sheet[i][6], H = client_sheet[i][7],
            I = client_sheet[i][8], J = client_sheet[i][9], K = client_sheet[i][10], L = client_sheet[i][11],
            M = client_sheet[i][12], N = client_sheet[i][13], O = client_sheet[i][14], P = client_sheet[i][15],
            Q = client_sheet[i][16], R = client_sheet[i][17], S = client_sheet[i][18], T = client_sheet[i][19],
            U = client_sheet[i][20], V = client_sheet[i][21], W = client_sheet[i][22], X = client_sheet[i][22],
            Y = client_sheet[i][23], Z = client_sheet[i][24], AA = client_sheet[i][25], AB = client_sheet[i][26],
        });
    }
    // MessageBox.Show(string.Format("{0}", client_sheet[0].Count));

    this.JumpTable.ItemsSource = entries; // Binds the entries to the datagrid
}
...