CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2010
    Posts
    56

    Lists to DataGridViews

    I am working on a datagridview that will update with object information.

    What I noticed is that you can use a List as a datasource. I basically have a list of objects that periodically check system status and show a GUI that indicates if something has failed.

    My question is how do you push object information to the columns of a datagridview using a datasource? I did some googling but really didnt find any good explanations.

  2. #2
    Join Date
    Apr 2012
    Posts
    29

    Re: Lists to DataGridViews

    You need to set up data-binding, expose what columns to be display (you can also copy them into a new class/struct properties) then bind them in the xaml.

  3. #3
    Join Date
    Jun 2010
    Posts
    56

    Re: Lists to DataGridViews

    Code:
                PropertyDescriptorCollection properties =
                    TypeDescriptor.GetProperties(typeof(T));
                DataTable table = new DataTable();
                foreach (PropertyDescriptor prop in properties)
                    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                foreach (T item in data)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
                return table;

    This is what I found online and it seems to work. You feed in the object that defines your list and it figures out the appropriate columns.

    So my process is load the list, load the table with modified code above, and then bind it to the data grid view.

  4. #4
    Join Date
    Dec 2009
    Posts
    145

    Re: Lists to DataGridViews

    Awesome! Here is another way to bind a list to datagrid using XAML
    Code:
     <DataGrid AutoGenerateColumns="False" Height="172" 
                          HorizontalAlignment="Stretch" 
                          Name="dgSkypeMate" VerticalAlignment="Top" HorizontalGridLinesBrush="#FFCECBCB"
                      VerticalGridLinesBrush="#FFCECBCB" MinRowHeight="10" FontFamily="Times New Roman"
                      FontSize="13" FontStretch="Normal"
                      Background="Azure" RowBackground="White"  AlternatingRowBackground="Lavender"
                      HorizontalScrollBarVisibility="Visible" 
                      VerticalScrollBarVisibility="Visible"
                      IsReadOnly="True" ItemsSource="{Binding}" 
                      Foreground="Black" SelectionChanged="dgSkypeMate_SelectionChanged" Margin="7,0,11,0" 
                      BorderBrush="#FFC4C4C4"
                      ItemContainerStyle="{StaticResource alternatingDataGridItemStyle}" 
                          UseLayoutRounding="True" 
                          ColumnHeaderHeight="33"
                          ContextMenuService.ShowOnDisabled="False"                      
                          >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Skypemate ID" MinWidth="100" Width="150" Binding="{Binding Path=idStr}" />
                        <DataGridTextColumn Header="Display name" Binding="{Binding Path=dispStr}" MinWidth="120" Width="220" />
                        <DataGridTextColumn Header="Last Online" MinWidth="100" Width="150" Binding="{Binding Path=lastOnlineStr}" />
                    </DataGrid.Columns>
                <DataGrid.ContextMenu>
                    <ContextMenu Name="conmenu" IsEnabled="True">
                        <MenuItem Header="Delete" Click="MenuItem_Click"/>
                    </ContextMenu>
                </DataGrid.ContextMenu>
            </DataGrid>
    That is a piece I took from my current tiny do-it-myself project. "Binding Path= [name of an existing object you want to display your data through and with its value]"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured