Click here to Skip to main content
15,794,236 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi people,

I have a problem with the Datagrid in WPF. I'm busy writing a WPF browser application. I have a datagrid declared on my page as follows:
<DataGrid  ItemsSource="{Binding}" AutoGenerateColumns="True" Height="462" Name="dgRepair" VerticalAlignment="Top" Grid.ColumnSpan="3" Margin="0,0,1,0" RowEditEnding="dgRepair_RowEditEnding" />

Then I have code to bind data from a table to this datagrid

SqlDataAdapter RepairAD;
DataSet repairDS;
        string sqlIns = @"select * from table";
        SqlCommand cmd = new SqlCommand(sqlIns, conn);
        RepairAD = new SqlDataAdapter(cmd);

        repairDS = new DataSet();
        RepairAD.Fill(repairDS, "Repair");
        dgRepair.DataContext = repairDS.Tables["Repair"].DefaultView;

What I need is code to update the table in the SQL database from the changes that are made in the datagrid.

I tried this code but it did nothing
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(RepairAD);
            RepairAD.Update(repairDS, "Repair");

If anyone can help it would be awesome.


ps: im using a datagrid and not a datagridview
Updated 2-Feb-21 3:09am

Take a look at:
WPF DataGrid Practical Examples[^]

Espen Harlinn
Share this answer
Venkatesh Mookkan 24-Mar-11 23:05pm    
Good answer!

And it is a nice reference man.
Espen Harlinn 25-Mar-11 4:23am    
Thank you, Venkatesh!
Sergey Alexandrovich Kryukov 30-Mar-11 15:31pm    
Agree, my 5, too.
Espen Harlinn 30-Mar-11 15:35pm    
Thanks SAKryukov!
Unforgiv3n 5-Apr-11 5:50am    
Great Answer!
Share this answer
CHill60 2-Feb-21 8:52am    
I've countered the downvote you received (probably for "answering" such an old question) - thank you for fixing the link

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