Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have been Bind the Image and other record in the Listview but i have problem in binding the record in DataGrid using linq to sql Please tell me how to bind the Image and first name in the DataGrid . I try to bind it but finally the result in my student table is null means no error but it show no record i am sure that i have some binding problem how can i solve it please please tell me and Thanks for reading . Here is My .xaml file of wpf please see and correct the errors on this fill
HTML
<Window x:Class="UI.ViewStudent"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:UI"Title="ViewStudent" Height="577" Width="415" xmlns:my="clr-      namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
        <Window.Resources >
            <local:ImageDataConverter x:Key="ImageData1Converter"/>
        </Window.Resources>
        <Grid Height="540">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="9*" />
                <ColumnDefinition Width="384*" />
            </Grid.ColumnDefinitions>
            <my:DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" Grid.Column="1" Margin="32,0,53,0" Name="dataGrid1" Height="200" VerticalAlignment="Top">
                <my:DataGrid.Columns>
                    <my:DataGridTemplateColumn Header="FirstName">
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                             <TextBox Text="{Binding Path=FirstName}" 
                                             Margin="-6,0,-6,0"/>
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>

                    <my:DataGridTemplateColumn Header="Imagess">
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Height="100" Source="{Binding Path=MyImage, Converter={StaticResource ImageData1Converter}}" />
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>
                </my:DataGrid.Columns>
            </my:DataGrid>
        </Grid >
    </Window>


I also try like this
HTML
<Grid Height="540">
            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="9*" />

                <ColumnDefinition Width="115*" />

                <ColumnDefinition Width="269*" />

            </Grid.ColumnDefinitions>

            <my:DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" Grid.Column="1" Margin="32,0,53,0" Name="dataGrid1" Height="200" VerticalAlignment="Top" Grid.ColumnSpan="2">
                <my:DataGrid.Columns>
                    <my:DataGridTextColumn Binding="{Binding FirstName}" Header="First Name"/>
                </my:DataGrid.Columns>
                <ItemsControl Name="imageItems">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                           <Image Height="100" Source="{Binding Path=MyImage, Converter={StaticResource ImageData1Converter}}" />

OR i do like that Path=
HTML
<Image Height="100" Source="{Binding Path= MyImage, Converter={StaticResource ImageData1Converter}}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </my:DataGrid>
    </Grid >
</Window>
But error is for
Path=<br />
    <Image Height="100" Source="{Binding Path= MyImage, Converter={StaticResource ImageData1Converter}}" /> 
that is Having no solution by doing this error is that Error
1 The Element type 'System.Windows.DataTemplate' does not have an associated TypeConverter to parse the string '<br />
                           Path='. Line 26 Position 35.<br />
    C:\Documents and Settings\Muhammad Yaqoob\My Documents\Visual Studio 2008\Projects\BLL\UI\ViewStudent.xaml
And my .xaml.cs file is
C#
namespace UI
    {
        public partial class Pics : Window
        {
            public Pics()
            {
                InitializeComponent();
            }
            private DataClasses1DataContext db = new DataClasses1DataContext();

            private BindingListCollectionView CustomerView;

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                var custsInCA = from c in db.Students
                                where c.StudentID  == 100
                                orderby c.LastName, c.FirstName
                                select c;

                this.DataContext = custsInCA;
                this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
            }
        }

        public class ImageDataConverter : IValueConverter
        {

             public object Convert(object value, Type targetType, object parameter,

             System.Globalization.CultureInfo culture)
             {
                System.Data.Linq.Binary binaryData = value as System.Data.Linq.Binary;
                //System.Data.Linq.Binary binaryData = value;// here there is the first error .How convert BinaryData to Object??
                if (binaryData == null)
                {
                    return null;
                }
                byte[] buffer = binaryData.ToArray();
                if (buffer.Length == 0)
                {
                    return null;
                }

                BitmapImage res = new BitmapImage();
                res.BeginInit();
                res.StreamSource = new System.IO.MemoryStream(buffer);
                res.EndInit();
                return res;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                    throw new NotImplementedException();
            }
        }
    }

In Short i want to Bind the Image by converting it from VarBinary to image which i converted in by using i value converter And the FirstName, LastName so on in the DataGrid using linq to sql wpf C# please if some one is expert for fixing the error or of some one know complete process of Binding the image and other record using linq to sql so please for good sack help me i am waiting for response of the noble and the good person please. your few minutes can solve my problem
Posted
Updated 11-Jan-12 8:11am
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