Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:33:27+00:00 2026-05-28T15:33:27+00:00

I have a listview and also a DataGrid having records with images. My problem

  • 0

I have a listview and also a DataGrid having records with images. My problem is that when I try to add the record in the listview or in the Datagrid I can only add the string or numbers only but can’t add an image. I tried to find it through an openfiledialog box and try to assign to the datagrid cell but i don’t know to what to assign the Image at run time if i search for the image. Another problem is that i have been binding the image of the Datagrid but i try to update it and add it at runtime .

And my second problem is updating the image. If any one know about the adding and updating the record in the listview and the also the Datagrid using template so please please for for the sack of God help me i try it but cant fix it . If any one who is expert of wpf please tell me your few minutes can solve my problem please.

My .xmal file is

<Window x:Class="UI.ViewClasses"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UI"
    Title="ViewClasses" Height="300" Width="456" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded">
    <Window.Resources >
        <local:ImageConverter x:Key="ImageDataConverter"/>
    </Window.Resources>
    <Grid>
        <my:DataGrid ItemsSource="{Binding}" Name="DataGrid" AutoGenerateColumns="False" Margin="12,51,80,35" SelectionMode="Extended" SelectionUnit="Cell" CanUserReorderColumns="True" CanUserResizeColumns="True" 
          CanUserResizeRows="False" CanUserSortColumns="True"  IsReadOnly="False" LoadingRow="DataGrid_LoadingRow"  AlternatingRowBackground="LightBlue"

         Loaded="DataGrid_Loaded">
            <my:DataGrid.Columns>

                <my:DataGridTemplateColumn Header="     Frist Name">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=FirstName}" 
                                         Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn Header="   Last Name">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=LastName}" 
                                         Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn Header="   Gender">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                    <TextBox Text="{Binding Path=Gender}" 
                                         Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>

                    <my:DataGridTemplateColumn Header="   GPA">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                           <TextBox Text="{Binding Path=GPA}" 
                                         Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                    <my:DataGridTemplateColumn Header="   Image">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Height="50" Name="image1" Source="{Binding Path=MyImage, Converter={StaticResource ImageDataConverter}}" />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>


            </my:DataGrid.Columns>
        </my:DataGrid>
        <Button Height="30" Margin="12,10,0,0" Name="btnAdd" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Click="btnAdd_Click">Add</Button>
        <Button Height="30" Margin="166,10,0,0" Name="btnSave" VerticalAlignment="Top" Click="btnSave_Click" HorizontalAlignment="Left" Width="75">Save</Button>
        <Button Height="30" HorizontalAlignment="Left" Margin="92,10,0,0" VerticalAlignment="Top" Width="75" Name="btnDelete" Click="btnDelete_Click">Delete</Button>
        <Button Height="30" HorizontalAlignment="Right" Margin="0,10,112,0" Name="Browses" VerticalAlignment="Top" Width="83" Click="Browses_Click">Brows</Button>
        <TextBox Height="30" HorizontalAlignment="Right" Margin="0,10,0,0" Name="txtBrowseFile" VerticalAlignment="Top" Width="110" />
    </Grid>
</Window>

And my .xaml.cs file is 

namespace UI
{
    /// <summary>
    /// Interaction logic for ViewClasses.xaml
    /// </summary>
    public partial class ViewClasses : Window
    {
        public ViewClasses()
        {
            InitializeComponent();
        } private DataClasses1DataContext db = new DataClasses1DataContext();

        private BindingListCollectionView CustomerView;

        private void Window_Loaded(object sender, RoutedEventArgs e)
         {
             DataClasses1DataContext db = new DataClasses1DataContext();
             var custsInCA = from c in db.Students                             
                             select c;

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

        }

        private void DataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
        { 


        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.db.SubmitChanges();
                MessageBox.Show("Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Student st = ((Student)(this.CustomerView.AddNew()));

            st.LastName = "<new>";
            this.CustomerView.CommitNew();
            this.DataGrid.ScrollIntoView(st);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if ((this.CustomerView.CurrentPosition > -1))
            {
                this.CustomerView.RemoveAt(this.CustomerView.CurrentPosition);
            }

        }

        private void Browses_Click(object sender, RoutedEventArgs e)
        {
            Image image1 = new Image();
            Microsoft.Windows.Controls.DataGrid df = new Microsoft.Windows.Controls.DataGrid(); 
            Microsoft.Win32.OpenFileDialog fileChooser = new Microsoft.Win32.OpenFileDialog();
            fileChooser.Filter = " Image files|*.jpg;*.gif;*.bmp;*.png;;*.jpeg";
            Nullable<bool> result = fileChooser.ShowDialog();
            if (result == true)

                try
                {

                    txtBrowseFile.Text = fileChooser.FileName;

                }
                catch { return; }
            if (txtBrowseFile.Text.Trim().Length != 0)
            {
                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.UriSource = new Uri(txtBrowseFile.Text.Trim(), UriKind.Relative);
                src.CacheOption = BitmapCacheOption.OnLoad;
                src.EndInit();

                image1.Source = src;
            }         
        }       
    }

    public class ImageConverter : 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();
        }
    }

}

My problem is that I want to add the record at runtime I can add the other record but cant image and also can’t update the image at run time in the datagrid. If there is any expert in the wpf then please replay my and replay me if possible in full detail that I can understand I have electric load shading problem in my country so I can’t quick respond to you so please if possible respond me for the sack of God I just wishing the noble persons those how helping me .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:33:29+00:00Added an answer on May 28, 2026 at 3:33 pm

    I have previously posted an answer with some info about INotifyPropertyChanged.

    how to add data to database and show it in datagrid without restarting program?

    Let me know if that is satisfacctory to your purpose of if you require additional help.

    EDIT

    One thing I would do is change the way you are working with your items.

        DataClasses1DataContext db = new DataClasses1DataContext();
        var custsInCA = from c in db.Students                             
             select c;
    
        this.DataContext = custsInCA;
        this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
    

    I generally use something more like this:

    public ObservableCollection<Student> Students = null;
    public CollectionViewSource cvs = new CollectionViewSource;
    public ViewClasses()
    {
        cvs.Source = Students;
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //Open Context
        DataClasses1DataContext db new DataClasses1DataContext();
        //Populate ObservableCollection with objects
        Students = 
            new ObservableCollection<Student>((from c in db.Students select c).ToList());
        //Raise PropertyChangedNotification
        OnPropertyChanged("Students");
    }
    

    And in XAML:

    <my:DataGrid ItemsSource="{Binding cvs}" Name="DataGrid">
    

    In this way, your DataGrids ItemsSource is bound directly to the CollectionViewSource, the CollectionViewSource is fed by the Students collection, and the change notification of change is sent up when the Students collection has been updated.

    The above code is just test code, there may be some spelling or punctuation mistakes. I am unfortunatly on the bus right now and not at my desk.

    Also, are you using Entity Framework?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview that contains values from webservice.Each page contains only 10 listitems
I have a ListView that displays error to the user. Part of it also
I have a listview with some items that can be marked as done. There
i have a listview and implemented onclick and also onfling.problem is when i do
i have a listview that displays images from a directory and it is loading
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have a ListView in WPF that is databound to a basic table that
I have a ListView on a page that displays a list of widgets. When
I have a listview with a DataTemplate that has a ComboBox. I want the
I have a ListView control that is in FullRowSelect mode, MultiSelect off and using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.