Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I have created simple wpf application having DataGrid Control.

binding of this is done well.

Now I want to perform insert , update delete operations using this DataGrid as we do in GridView.


Please guide me how can i achieve this.


Thanks in advance.
Posted
Updated 24-Sep-21 21:01pm
v2
Comments
Wayne Gaylard 13-Jun-11 6:40am    
You need to give us more information with regard to how you are binding the datagrid. Do you bind to a DataTable, DataSet, or a collection of .Net objects? Do you want to update the database as soon as the user is finished entering data, or do you want the user to press a button once they have finished all updates, or what ?
Vivek Deshmukh 13-Jun-11 7:12am    
I have bind the DataGrid by using dataset as
DG_Company.ItemsSource = ds_Company.Tables[0].DefaultView;
When i run program my DG_Company datagrid displays all records from dataset.

If i edit some rows from datagrid at that time this changes has to be save in database.

So for this what do i do ?

Here i have done insert and update operation using sp in datagrid...
But I am getting an error ...No mapping exists from object type System.Windows.Controls.DataGridTextColumn to a known managed provider native type.'

Can AnyOne Solve This Problem?
Thank You ..

namespace DataGrid_StoreProcedure
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
string cs = ConfigurationManager.ConnectionStrings["DBMS"].ConnectionString;

public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{



PopulateDatagrid();


}

void PopulateDatagrid()
{

using (SqlConnection con = new SqlConnection(cs))
{

con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * from Emp", con);
DataTable dt = new DataTable();
sda.Fill(dt);
Mydg.ItemsSource = dt.AsDataView();


}
}

private void Mydg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{


if (Mydg.SelectedIndex == 0)
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();

SqlCommand cmd = new SqlCommand("SPemployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "INSERT");
cmd.Parameters.AddWithValue("@EmployeeId", 0);
cmd.Parameters.AddWithValue("@FirstName", FirstName);
cmd.Parameters.AddWithValue("LastName", LastName);
cmd.Parameters.AddWithValue("@Age", Age);
cmd.Parameters.AddWithValue("@IsAvailable", IsAvailable);
cmd.ExecuteNonQuery();
con.Close();

}
}
else
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand("SPemployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "UPDATE");
cmd.Parameters.AddWithValue("@EmployeeId", SqlDbType.Int).Value = EmployeeId;
cmd.Parameters.AddWithValue("@FirstName", SqlDbType.NVarChar).Value = FirstName;
cmd.Parameters.AddWithValue("LastName", SqlDbType.NVarChar).Value = LastName;
cmd.Parameters.AddWithValue("@Age", SqlDbType.Int).Value = Age;
cmd.Parameters.AddWithValue("@IsAvailable", SqlDbType.Bit).Value = IsAvailable;
cmd.ExecuteNonQuery();




}


}


PopulateDatagrid();





}
}
}
 
Share this answer
 
Comments
Richard Deeming 27-Sep-21 10:05am    
If you want to ask a question, then ASK A QUESTION[^]. Do not post your question as a "solution" to someone else's question.
Hope DataGrid in WPF using SQL Server Compact 3.5 Sp1[^] article from CP might help you.
 
Share this answer
 
Comments
Vivek Deshmukh 13-Jun-11 6:30am    
But i need to do this without using third component. As here TextBox values added in database. I need to edit in same row .

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