Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Winforms : Datagrid Eventhandling + Update

Posted on 2004-09-08
11
Medium Priority
?
359 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 1800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

963 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