Welcome to the Xceed Community | Help
Community Search  
More Search Options

DataBind with LINQ

Sort Posts: Previous Next
  •  02-04-2009, 6:15 PM Post no. 18416

    DataBind with LINQ

    Hello guys,

    How can I bind the grid from a linq query that return a List<T>?
    Using an anonymous type, the datagrid shows the data correctly, but it´s a pain to get a the values from a selected item.

    Here is my XAML code:

                     <xcdg:DataGridControl Grid.Row="2" Visibility="Hidden" Grid.Column="0" Height="Auto" Width="Auto" ReadOnly="True" x:Name="gridUsuarios" AutoCreateColumns="False" MouseDoubleClick="gridUsuarios_MouseDoubleClick">
                        <xcdg:DataGridControl.Columns>
                            <xcdg:Column FieldName="CdRE" Title="RE" IsMainColumn="True" Width="100" />
                            <xcdg:Column FieldName="NmUsuario" Title="Nome" Width="300" IsMainColumn="False" />
                            <xcdg:Column FieldName="CdUnidade" Title="Código Unidade" />
                            <xcdg:Column FieldName="NmUnidade" Title="Nome Unidade" />
                            <xcdg:Column FieldName="DtDesativacao" Title="Data da Desativação" />
                            <xcdg:Column FieldName="NmPerfil" Title="Perfil" Width="200" />
                        </xcdg:DataGridControl.Columns>
                        <xcdg:DataGridControl.Resources>
                            <Style x:Key="{x:Type xcdg:ScrollTip}" TargetType="xcdg:ScrollTip">
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="HorizontalAlignment" Value="Right" />
                            </Style>
                        </xcdg:DataGridControl.Resources>
                        <xcdg:DataGridControl.View>
                            <xcdg:TableView IsAlternatingRowStyleEnabled="True" UseDefaultHeadersFooters="False" ShowScrollTip="True" ShowFixedColumnSplitter="False">
                                <xcdg:TableView.FixedHeaders>
                                    <DataTemplate>
                                        <xcdg:ColumnManagerRow />
                                    </DataTemplate>
                                </xcdg:TableView.FixedHeaders>
                                <xcdg:TableView.Theme>
                                    <xcdg:AeroNormalColorTheme />
                                </xcdg:TableView.Theme>
                            </xcdg:TableView>
                        </xcdg:DataGridControl.View>
                    </xcdg:DataGridControl>

     and here is the CS file:

                 using (db.dbDataContext dc = new db.dbDataContext())
                {
                    var q1 = from u in dc.Usuarios
                             select new ItemGrid
                             {
                                 CdRE = u.CdRE,
                                 NmUsuario = u.NmUsuario,
                                 CdUnidade = u.CdUnidade,
                                 NmUnidade = u.Unidade.NmUnidade,
                                 DtDesativacao = u.DtDesativacao,
                                 NmPerfil = u.PerfilUsuario.NmPerfil
                             };

                    gridUsuarios.ItemsSource = q1.ToList();
                }

            public class ItemGrid
            {
                public string CdRE;
                public string NmUsuario;
                public decimal CdUnidade;
                public string NmUnidade;
                public DateTime? DtDesativacao;
                public string NmPerfil;
            }

    Regards,

     Henrique

    Filed under: ,
  •  02-07-2009, 4:48 PM Post no. 18476 in reply to 18416

    Re: DataBind with LINQ

    I have the same question I would like to recover the value of one of the fields in the datagrid when a row is selected so that I can pass it to another linq query to populate another grid.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.