Solved

C# Winforms : Datagrid Eventhandling + Update

Posted on 2004-09-08
11
325 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now