Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
below code is ObservableCollection in binding data in wpf C#.
so tell me how to implement below code in add update delete button ..

click on add button all data like (name, fathername, DOB, Gender, State, District fill up detail) in show datagridview .

What I have tried:

xaml code

<Window x:Class="Wpf1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        xmlns:local="clr-namespace:Wpf1"
        ResizeMode="NoResize"
        SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen"
        Height="1000" Width="1100">


  <window.resources>
    <local:EnumBooleanConverter  x:Key="EnumBooleanConverter"/>
  

  <grid>
    <grid.columndefinitions>
      <ColumnDefinition Width="481*" />
      <ColumnDefinition Width="511*" />
    
    <Grid Height="421" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="411">
      <grid.rowdefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="42*" />
        <RowDefinition Height="44*" />
        <RowDefinition Height="43*" />
        <RowDefinition Height="46*" />
        <RowDefinition Height="45*" />
        <RowDefinition Height="45*" />
        <RowDefinition Height="44*" />
        <RowDefinition Height="62*" />
      
      <grid.columndefinitions>
        <ColumnDefinition Width="104*" />
        <ColumnDefinition Width="307*" />
      

      

      <TextBox Height="23" HorizontalAlignment="Left" Margin="3,27,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="211" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Grid.Column="1"/>

      

      <TextBox Height="23" HorizontalAlignment="Left" Margin="2,14,0,0" Name="FathernameTextBox" VerticalAlignment="Top" Width="211" Text="{Binding FatherName, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Column="1" Grid.Row="1"/>

      

      <DatePicker Height="25" HorizontalAlignment="Left" Margin="3,19,0,0" Name="DOBDatePicker" VerticalAlignment="Top" Width="211" Text="{Binding DOB, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Column="1" Grid.Row="2"/>

      

      <RadioButton Content="Male" Margin="3,27,0,0" Name="MaleRadioButton" IsChecked="{Binding Gender, Mode=TwoWay,UpdateSourceTrigger=LostFocus, Converter = {StaticResource EnumBooleanConverter}, ConverterParameter=Male}" Grid.Column="1" HorizontalAlignment="Left" Width="42" Grid.Row="3" />

      <RadioButton Content="Female" Height="16" HorizontalAlignment="Left" Margin="56,27,0,0" Name="FemaleRadioButton" VerticalAlignment="Top" IsChecked = "{Binding Gender, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Female}" Grid.Column="1" Grid.Row="3" />

      

      <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,23,93,0" Name="StateComboBox" VerticalAlignment="Top" Width="211" Text="{Binding State, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Row="4" Grid.Column="1">
        <ComboBoxItem Content="Gujarat"/>
        <ComboBoxItem Content="Rajesthan"/>
        <ComboBoxItem Content="dilhi"/>
      

      

      <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,22,93,0" Name="DistrictComboBox" VerticalAlignment="Top" Width="211" Text="{Binding District, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Row="5" Grid.Column="1">
        <ComboBoxItem Content="X"/>
        <ComboBoxItem Content="Y"/>
        <ComboBoxItem Content="Z"/>
      

      

      

      

    
    <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="34,123,0,0" Name ="PersonDatagrid" VerticalAlignment="Top" Width="502"  Grid.Column="1">

      <datagrid.columns>
        <DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Fathername" Width="100" Binding="{Binding FatherName}"/>
        <DataGridTextColumn Header="DOB" Width="70" Binding="{Binding DOB, StringFormat=dd/MM/yyyy}"/>
        <DataGridTextColumn Header="Gender" Width="50" Binding="{Binding Gender, Converter={StaticResource EnumBooleanConverter}}"/>
        <DataGridTextColumn Header="State" Width="80" Binding="{Binding State}"/>
        <DataGridTextColumn Header="District" Width="98" Binding="{Binding District}"/>

      
    
  
</Window

xaml.cs

 public class personalinfo
    {
        public string Name { get; set; }
        public Genders Gender { get; set; }

        public string State { get; set; }

        public string District { get; set; }

        public string DOB { get; set; }

        public string Fathername { get; set; }
    }

   public enum Genders
   {
       Male = 1,
       Female
   }
}

 public  class EnumBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object paramvalue = Enum.Parse(value.GetType(), ParameterString);
            return paramvalue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            return Enum.Parse(targetType, ParameterString);
        }

    }

}


namespace Wpf1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            PersonDatagrid.ItemsSource = new List<personalinfo> 
            
            { 
                new personalinfo {Name = "Mariya", Fathername = "shakti", DOB = "26/12/1992", Gender = Genders.Male, State = "Gujarat", District="Baroda" }  
            };
        }
            private void SaveButton_Click(object sender, RoutedEventArgs e)
            {
                //personalinfo.Add(new Button() {Content = $"Button {index++}"});
                Console.WriteLine(string.Format("You clicked on the {0}. button.", (sender as Button).Tag));
             }

            private void DeleteButton_Click(object sender, RoutedEventArgs e)
            {

            }

            private void RefreshButton_Click(object sender, RoutedEventArgs e)
            {

            } 
    }
}
Posted
Updated 28-Jan-17 5:09am
v2
Comments
[no name] 28-Jan-17 8:25am    
The best thing you can do right now is stop. Stop and get yourself a book on basic WPF programming and work through it. Copying and pasting bits and pieces from the internet is not going to teach you WPF or programming.
jay_1493 29-Jan-17 23:42pm    
Thank u help you
jay_1493 30-Jan-17 0:01am    
please suggest website in basic WPF Programming !!
[no name] 30-Jan-17 13:51pm    
If you can't find anything on www.codeproject.com then try www.google.com

If you properly construct your viewmodel and bind it to the control, all you have to do is modify the viewmodel, and the control will automagically update itself when it detects a change to the items in the bound list. I'm hoping your instructor went over this with you before assigning this homework.

If it is not indeed homework, you should consider a new career.
 
Share this answer
 
v2
I don't see ObservableCollection being used in your .cs code.
See example here: How to: Create and Bind to an ObservableCollection[^]
And here: Mitesh Sureja's Blog: Binding ObservableCollection to ListBox in WPF[^]
 
Share this answer
 

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