Link to home
Start Free TrialLog in
Avatar of Jzzzz
JzzzzFlag for United States of America

asked on

C# Winforms : Datagrid Eventhandling + Update

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

ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jzzzz

ASKER

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"
Avatar of Jzzzz

ASKER

please ignore the last few lines starting
" when the app runs...."

i started writing that when the update didnt work..

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.
Avatar of Jzzzz

ASKER


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?



Hmmm. When you load the data, does the text box show the value of the first row in the table?
Avatar of Jzzzz

ASKER

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" ....
Avatar of Jzzzz

ASKER

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..

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....
Avatar of Jzzzz

ASKER

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
You are welcome. Waiting for the tough one :-))