Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts
I am New to WPF. I have a form that contains a Datagrid with autogenratecolumns false.I have 6 columns .I want insert data into sql table through these columns. I have button "Add More" by this i want to add a new row in datagrid .I want to insert all data of DataGrid By Submit Button...

Plz Help..

Thanks In Advance.
Posted
Updated 4-Sep-19 22:36pm
v2
Comments
Naz_Firdouse 10-Jul-13 7:49am    
what have you tried?
Sadique KT 11-Jul-13 1:07am    
How is data binding? collection or table ? post the code..
ErBhati 11-Jul-13 1:18am    
insert data in txt box of the datagrid then the value insert into table

Try the following link

WPF DataGrid Practical Examples[^]
 
Share this answer
 
v2
Comments
ErBhati 11-Jul-13 5:22am    
I am asking How i do this in WPF not in simple asp.net gridview....
Deenuji 11-Jul-13 5:25am    
i updated my solution check it now
<Window x:Name="Main" x:Class="DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="623" Loaded="Main_Loaded">
    <Grid>
        <DataGrid x:Name="dgProducts" HorizontalAlignment="Left" Margin="10,31,0,0" VerticalAlignment="Top"
                   ItemsSource="{Binding}"   AutoGenerateColumns="False" RowEditEnding="dgProducts_RowEditEnding" AddingNewItem="dgProducts_AddingNewItem" BeginningEdit="dgProducts_BeginningEdit" PreviewKeyDown="dgProducts_PreviewKeyDown" Width="595" AlternatingRowBackground="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    Width="75" Header="Product ID" IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding ProductCode, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    Width="100" Header="Code"/>
                <DataGridTextColumn Binding="{Binding ProductDescription, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                    Width="100" Header="Description"/>
                <DataGridTextColumn  Binding="{Binding ProductPrice, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                    Width="100" Header="Price"/>
                <DataGridTemplateColumn Header="Expiration date">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ProductExpirationDate, StringFormat=\{0:d\}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate ="{Binding ProductExpirationDate, NotifyOnSourceUpdated=True,Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Binding="{Binding IsBio, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                    Width="100" Header="Bio"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Step 5: Code file:

using DataGrid.DAL;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        InvoicesEntities context = new InvoicesEntities();
        bool isInsertMode = false;
        bool isBeingEdited = false;

        public MainWindow()
        {
            InitializeComponent();
        }
        private void Main_Loaded(object sender, RoutedEventArgs e)
        {
            dgProducts.ItemsSource = GetProductList();
        }
        private ObservableCollection<Products> GetProductList()
        {
            var list = from e in context.Products select e;
            return new ObservableCollection<Products>(list);
        }
        private void dgProducts_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            Products product = new Products();
            Products curProd = e.Row.DataContext as Products;
            if (isInsertMode)
            {
                var InsertRecord = MessageBox.Show("Do you want to add " + curProd.ProductCode  + " as a new product?", "Confirm", MessageBoxButton.YesNo,

                    MessageBoxImage.Question);
                if (InsertRecord == MessageBoxResult.Yes)
                {
                    product.ProductCode = curProd.ProductCode;
                    product.ProductDescription = curProd.ProductDescription;
                    product.ProductPrice = curProd.ProductPrice;
                    product.ProductExpirationDate = curProd.ProductExpirationDate;
                    context.Products.Add(product);
                    context.SaveChanges();
                    dgProducts.ItemsSource = GetProductList();
                    MessageBox.Show(product.ProductCode + " " + product.ProductDescription + " has being added!", "Add product", MessageBoxButton.OK, MessageBoxImage.Information);
                    isInsertMode = false;

                }
                else
                    dgProducts.ItemsSource = GetProductList();
            }
            context.SaveChanges();
        }
        private void dgProducts_AddingNewItem(object sender, AddingNewItemEventArgs e)
         {
            isInsertMode = true;
        }
        private void dgProducts_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            isBeingEdited = true;
        }
        private void dgProducts_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete && !isBeingEdited)
            {
                if (dgProducts.SelectedItems.Count > 0)
                {
                    var Res = MessageBox.Show("Are you sure you want to delete " + dgProducts.SelectedItems.Count + " products?", "Deleting products",
                        MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                    if (Res == MessageBoxResult.Yes)
                    {
                        foreach (var row in dgProducts.SelectedItems)
                        {
                            Products product = row as Products;
                            context.Products.Remove(product);
                        }
                         context.SaveChanges();
                        MessageBox.Show(dgProducts.SelectedItems.Count + " Products have being deleted!");
                    }
                    else
                        dgProducts.ItemsSource = GetProductList();
                }
            }
        }
    }
}
 
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