Click here to Skip to main content
15,359,445 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:
XML
<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

C#
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;
        dgRepair.UpdateLayout();


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.

Thanks

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

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

Regards
Espen Harlinn
   
Comments
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.
--SA
Espen Harlinn 30-Mar-11 15:35pm
   
Thanks SAKryukov!
Unforgiv3n 5-Apr-11 5:50am
   
Great Answer!
Espen Harlinn 5-Apr-11 14:14pm
   
Thanks for the reply, nice that you liked the article :)
   
Comments
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