Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

try this code
datagrid.Items.Refresh();
 
Share this answer
 
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
{
///
/// 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;

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