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>
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)
{
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)
{
}
}
}