Click here to Skip to main content
13,298,361 members (58,535 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to refresh the rows in datagrid in wpf application...I had perform insert,update and delete operations using queries and database has changed but no updations occured on the i can perform that using c#..
Thanks in Advance
Posted 17-Dec-12 23:53pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

try this code
A C swathi 18-Dec-12 5:29am
Thanks..i have applied this.But still it is not working ...there is no change in the datagrid
krushna chandra jena 18-Dec-12 5:44am
After insert or delete operation you can perform select query , so that whatever changes have done in database, may show in User Interface also
A C swathi 18-Dec-12 12:15pm
Thanks.I tried it..but the selected row is not deleting from datagrid after performing the delete operations.Is there any solution for that..
krushna chandra jena 19-Dec-12 0:37am
After inserting, you must call bindgrid() method in button1_click().
A C swathi 18-Dec-12 22:45pm
This is my program...could you please help me to delete a selected row from grid after delete operation in this program...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data.SqlClient;
using System.Data;

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

public MainWindow()

private void bindgrid()

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;User ID=sa;Password=nest123@!");
string cmd1 = "select * from book2";
SqlCommand cmd = new SqlCommand(cmd1, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable("book2");

dataGrid1.ItemsSource = dt.DefaultView;

private void button1_Click(object sender, RoutedEventArgs e)
DataTable dt=new DataTable();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;User ID=sa;Password=nest123@!");
object item=dataGrid1.SelectedItem;
string col1 = (dataGrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
string col2 = (dataGrid1.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text;
string col3 = (dataGrid1.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
string col4 = (dataGrid1.SelectedCells[3].Column.GetCellContent(item) as TextBlock).Text;
string p = "insert into book2 values('" + col1 + "','"+col2+"','"+col3+"','"+col4+"')";
SqlCommand cmd = new SqlCommand(p, con);

catch (SqlException e1)
MessageBox.Show("value inserted");
private void button2_Click(object sender, RoutedEventArgs e)
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;User ID=sa;Password=nest123@!");


object item = dataGrid1.SelectedItem;
string bookid = (dataGrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
string name = (dataGrid1.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text;
string author = (dataGrid1.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
string prize = (dataGrid1.SelectedCells[3].Column.GetCellContent(item) as TextBlock).Text;

SqlCommand cmd = new SqlCommand("update book2 set name=@name,author=@author,prize=@prize where bookid=@bookid", con);
cmd.Parameters.AddWithValue("@bookid", SqlDbType.NVarChar).Value = bookid;
cmd.Parameters.AddWithValue("@name", SqlDbType.NVarChar).Value = name;
cmd.Parameters.AddWithValue("@author", SqlDbType.NVarChar).Value = author;
cmd.Parameters.AddWithValue("@prize", SqlDbType.NVarChar).Value = prize;
A C swathi 19-Dec-12 1:03am
Thanks....i tried it...but no updation is occuring in datagrid after there any method for selecting a rowindex and based on that we can perform delete dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index)in windows application
krushna chandra jena 19-Dec-12 1:39am
you can use

or you can use
var dg = dataGrid1.CurrentItem as DataRowView;
string deleteFilePath = dg[0].ToString();
A C swathi 19-Dec-12 4:38am
Thank u...its working now...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100