Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When I bind datagrid in wpf with of linq lambda expression its fill datagrid but when I try to convert this linq result in datatable then its fill null datagrid below my code which I tried ....
C#
var join = dm.Export_Matrilas_s
             .Where(rh => rh.Material_type == 5)
             .Join(dm.Parti_Infos, p => p.Parti_Id, w => w.Parti_Id, (p, w) => new { p.Export_Matrilas_id, p.Material_type, p.Owncom, w.Organization, p.Date })
             .Join(dm.Own_Company_Infos, r => r.Owncom, y => y.Company_Id, (r, y) => new { r, y.Organization_Name })
             .Join(dm.Material_Types, n => n.r.Material_type, b => b.Id, (n, t) => new { n.r.Export_Matrilas_id, t.Meterial_Name, n.Organization_Name, n.r.Organization, n.r.Date })
             .ToList();

DataTable gettbl = ToDataTable(join);
mygride.DataContext = gettbl.DefaultView;



Here I convert into datatable using method


C#
public DataTable ToDataTable<T>(IList<T> data)// T is any generic type
  {
      PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

      DataTable table = new DataTable();
      for (int i = 0; i < props.Count; i++)
      {
          PropertyDescriptor prop = props[i];
          // table.Columns.Add(prop.DisplayName,prop.PropertyType);
          table.Columns.Add(prop.DisplayName, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
      }
      object[] values = new object[props.Count];
       DataRow row;

      foreach (var record in data)
      {
          row = table.NewRow();
          for (int i = 0; i < table.Columns.Count; i++)
          {
              values[i] = props[i].GetValue(record) != null ? props[i].GetValue(record) : DBNull.Value;
          }

          table.Rows.Add(row);
      }

      table.Rows.Add(values);

      return table;
  }

Here is my xaml code:
HTML
<UserControl x:Class="Iqbal_Silks.Export_Tanee_Records"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             Height="700" Width="900">
    <UserControl.Resources>
        <ResourceDictionary Source="../Themes/Dictionary1.xaml"/>
    </UserControl.Resources>

    <Grid >
     <DataGrid x:Name="mygride" Margin="3,243,10,64" AutoGenerateColumns="False" AlternatingRowremoved="AliceBlue" VerticalContentAlignment="Center" CanUserResizeRows="False"   HorizontalContentAlignment="Center" ScrollViewer.PanningMode="VerticalFirst" IsReadOnly="True">
            <DataGrid.Columns>

                <DataGridTextColumn Header="Export ID" Width="Auto" Binding="{Binding Export_Matrilas_id}"/>
                <DataGridTextColumn Header="Materials " Width="Auto" Binding="{Binding Meterial_Name}" />
                <DataGridTextColumn Header="Company" Width="Auto" Binding="{Binding Organization_Name}"/>
                <DataGridTextColumn Header="Client" Width="Auto" Binding="{Binding Organization}"/>
                <DataGridTextColumn Header="Date" Width="Auto" Binding="{Binding Date}"/>

            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Excel Records" x:Name="btnviewchallan_Copy"  Style="{StaticResource Hdrclose}" HorizontalAlignment="Left" Margin="616,641,0,0" VerticalAlignment="Top" Width="94" Height="27" Click="BtnviewchallanCopyClick" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="10,644,768,32">
            <Button Name="btnFirstPage" Content="<<" Click="btnFirstPage_Click"/>
            <Button Name="btnPreviousPage" Content="<" Click="btnPreviousPage_Click"/>
            <Label Name="lblPageIndex" Content="{Binding ElementName=root,
                Path=PageIndex, UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
            <Label Content="of"/>
            <Label Name="lblPageNumber" Content="{Binding ElementName=root,
                Path=NumberOfPages, UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
            <Button Name="btnNextPage" Content=">" Click="btnNextPage_Click"/>
            <Button Name="btnLastPage" Content=">>" Click="btnLastPage_Click"/>
        </StackPanel> 
</Grid>
Posted
Updated 13-Jan-15 5:04am
v2

1 solution

I think your ToDataTable method has a bug in the foreach loop. Try this version.


C#
public DataTable ToDataTable<t>(IList<t> data)// T is any generic type
       {
           PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

           DataTable table = new DataTable();
           for (int i = 0; i < props.Count; i++)
           {
               PropertyDescriptor prop = props[i];
               // table.Columns.Add(prop.DisplayName,prop.PropertyType);
               table.Columns.Add(prop.DisplayName, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
           }
           object[] values = new object[props.Count];
           foreach (var record in data)
           {
               for (int i = 0; i < table.Columns.Count; i++)
               {
                   values[i] = props[i].GetValue(record) != null ? props[i].GetValue(record) : DBNull.Value;
               }

               table.Rows.Add(values);
           }



           return table;
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900