Solved

C# Winforms : Datagrid Eventhandling + Update

Posted on 2004-09-08
11
340 Views
Last Modified: 2012-08-13
Hi,

I am new to .Net. and i am writing a winforms app for adding and updating records.

I have a dataset with 2 tables. A parent table & associated child table.

I created a data-relation, so that when a record on the parent table is selected (the + sybmbol clicked) it reveals  the corresponding rows of the child table .

this is wat i did
***********************************************************************

     sqlDataAdapter1.Fill(dataSet1,"table1");
     sqlSelectCommand1.CommandText = "select * from [table1]";
     sqlDataAdapter1.Fill(dataSet1,"table2");
               DataRelation myDr =  new DataRelation("myRelation",dataSet1.Tables[0].Columns["ID"],dataSet1.Tables[1].Columns["ID"]);
     dataSet1.Relations.Add(myDr);
     dataGrid1.DataSource = dataSet1.DefaultViewManager;
     dataGrid1.DataMember = "table1";

***********************************************************************


1)What i need is to display the parent tables record( one field) in a text box when its selected , i.e when the child records are displayed.
and
2)Allow the users to make changes to both these tables and Update them ensuring that updated information is valid (basic validation).

guess i need an update method when a 'save changes' button is clicked .

but what do i need to do in the update method to save changes and how to display that field when that record is selected?

thanks,
Jz

0
Comment
Question by:Jzzzz
  • 6
  • 5
11 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 450 total points
ID: 12016209
1) Create a text box and set its DataBinding properties: set the data source to the parent table and the data member to the filed you want to show. It should work immediately

2) It is better to use two different data adapters to fill the data. Then you can also use the same adapters with their Update methods to update the data. The adapter will automatically decide which records have to be updated, which are new and which are deleted.

I don't know if you created your data adapter with the designer or manually. If with the designer, just create another one for the second table and don't set the CommandText in the code manually. If you created it manually, you have to create the other one also manually. Use the SqlCommandBuilder (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlcommandbuilderclasstopic.asp) for that, so that all commands are created.
0
 

Author Comment

by:Jzzzz
ID: 12018477
1) I created a new adapter for the child table...bs.net created a new connection as well. i just left it to do that.
i used to vs.net for both adapters.

then,i did this is in  savebtn_click():

savebtn_click(){
         SqlCommandBuilder cb = new SqlCommandBuilder(sqlDataAdapter1);
                   SqlCommandBuilder cb2 = new SqlCommandBuilder(sqlDataAdapter2);
                                                                sqlDataAdapter1 .Update(dataSet1,"table1");
         sqlDataAdapter2 .Update(dataSet1,"table2");

}

*********************update works super***********************
So if i have more tables ""do i need a Adapter and Commnadbuilder for each table to get updated""  as
                     adapter1.update(ds,table1)
                     adapter2.update(ds,table2)
                    adapter3.update(ds,table3)    ???

2) for textbox databinding i did this as you said

 textBox1.DataBindings.Add("Text",dataSet1.table2,"field");

here i want the text box to display to display the "field" -- only when i navigate to the child table and to whicever record.

But this doesnt work...it displays some erred record and

will the item in the textbox change when i move from one record to another while browsing the childtable ????
and if i make changes to the textbox will that be updated to the dataset and hence taken care automatically in savebtn_click() when i do adapter.update(dataset,table) ????


when the app runs and i edit a record change a value and hit "savebtn".. it doesnt updtae and gives an error..it adds new record with the same P'key and displays this error on the datagrid
""

waiting for ur reply on this
thanksa bunch "Avenger"
0
 

Author Comment

by:Jzzzz
ID: 12018512
please ignore the last few lines starting
" when the app runs...."

i started writing that when the update didnt work..

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 20

Expert Comment

by:TheAvenger
ID: 12018518
OK, you don't have to make all these things manually. If you build the adapters with the designer, you only need to call the Update method, no need for SqlCommandBuilder. Also the designer will use the same connection if it points to the same server/database, so something is wrong with your situation. And finally the databinding of the text box - just select the text box in the designer and have a look at its properties (Alt+Enter if you don't have them on the screen). Find the group DataBindings and fill in the fields there. Then delete your manual code.

The VS designer is a very powerful tool, don't underestimate it and use it whenever possible.
0
 

Author Comment

by:Jzzzz
ID: 12019160

1) Now i redid the whole thing.

used VS.net for 2 adapter and i didnt do any coding.

now i just do  

adapter1.update(ds, table1 )
adapter2.update(ds, table2)

cool """"""""""""""""Update works fine""""""""""""""""

2) for the 'textbox',  i set the DataBindings group in VS designer to the field in table in the dataset..

   windows desinger generated this code: (table2 is the child table )

   **********************************************
   this.my_txtBox.DataBindings.Add(new System.Windows.Forms.Binding("Text",                  this.dataSet1, "table2.myfield"));

   **********************************************

------But the field is not displayed correctly.. it displays some value  even when the parent-table is shown in the datagrid when the FORM opens first...----------

and like i asked before

a) will the item in the textbox change when i move from one record to another while browsing the childtable ????  it should i guess..

b) and if i make changes to the textbox will that be updated to the dataset and hence taken care automatically in savebtn_click() when i do adapter.update(dataset,table) ????

If this works.. the big question

what if table1 and table2 are many to many:
shall i post that as another thread?



0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12019242
Hmmm. When you load the data, does the text box show the value of the first row in the table?
0
 

Author Comment

by:Jzzzz
ID: 12019738
srry was out for lnch

it shows a jumbled value of the field i am expecting...
and doesnt change when i move through the records..

say i have a entry like "AAAAA------bbbbb______-NNNNNNNNNN" ---in the child table

it displayss  "_____________NNNN" ....
0
 

Author Comment

by:Jzzzz
ID: 12020734
avenger,

guess what..

as i am updating the  table through a datadapter for each of them, the many-many relationship doesnt come into the picture at all..

Below- the child to grandchild is a     many - many :

parentTable -
                  childTable  -
                                      grandchildTable

earlier I updated all this through one Adapter and......i guess.. i got the unique-key not present error...
now since i am using an adapter for each table...there is no parent-child enforcement..

Am I right ?

and that textbox thing aint  getting better .....

r u there?
pls just clear me on this.. wont take no more of ur time..

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12021054
Sorry, I am in Europe and it's somewhat during the late evening at the moment :-))

OK, I didn't get your idea fully but in general you have to fill in the data correctly in all the tables so that the records are correct also in the database. The update method will just send the data from your dataset to the database. That's all. You have to care about linking the correct records in the dataset so that they are also linked in the database.

For the text box: it seams that something is wrong, but I can't see what.

Is it possible for you to put your solution somewhere on the web so that I can download it and have a closer look? Including the DB if possible....
0
 

Author Comment

by:Jzzzz
ID: 12021366
oh..i thot u were in west US all the while..

Cool..

i got the picture about Dataadapters, datarelation and updates..

as for the TextBox.i shall mess around and find my way thru..

and be back with a much tougher question for you..:))

thanks a lot budd.

Jz
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12021482
You are welcome. Waiting for the tough one :-))
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question