Click here to Skip to main content
12,634,107 members (23,807 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF
hi...
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 datagrid...how i can perform that using c#..
Thanks in Advance
swathi
Posted 17-Dec-12 23:53pm

1 solution

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

Solution 1

try this code
datagrid.Items.Refresh();
  Permalink  
Comments
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()
{
InitializeComponent();
bindgrid();


}
private void bindgrid()
{

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


DataTable dt = new DataTable("book2");

sda.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;

}
//insert
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@!");
con.Open();
try
{
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);

cmd.ExecuteNonQuery();
}
catch (SqlException e1)
{
MessageBox.Show(e1.Message.ToString());
}
MessageBox.Show("value inserted");
dataGrid1.Items.Refresh();
con.Close();
}
//update
private void button2_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;User ID=sa;Password=nest123@!");

con.Open();
try
{


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 deletion..is there any method for selecting a rowindex and based on that we can perform delete operation.like dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index)in windows application
krushna chandra jena 19-Dec-12 1:39am
   
you can use
dataGrid1.Items.RemoveAt(dataGrid1.SelectedIndex);

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 | Mobile
Web02 | 2.8.161208.2 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2016
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