Jzzzz
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(dataS et1,"table 1");
sqlSelectCommand1.CommandT ext = "select * from [table1]";
sqlDataAdapter1.Fill(dataS et1,"table 2");
DataRelation myDr = new DataRelation("myRelation", dataSet1.T ables[0].C olumns["ID "],dataSet 1.Tables[1 ].Columns[ "ID"]);
dataSet1.Relations.Add(myD r);
dataGrid1.DataSource = dataSet1.DefaultViewManage r;
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
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(dataS
sqlSelectCommand1.CommandT
sqlDataAdapter1.Fill(dataS
DataRelation myDr = new DataRelation("myRelation",
dataSet1.Relations.Add(myD
dataGrid1.DataSource = dataSet1.DefaultViewManage
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
please ignore the last few lines starting
" when the app runs...."
i started writing that when the update didnt work..
" 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.
The VS designer is a very powerful tool, don't underestimate it and use it whenever possible.
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.DataBinding
**************************
------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,tab
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?
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______-NN NNNNNNNN" ---in the child table
it displayss "_____________NNNN" ....
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______-NN
it displayss "_____________NNNN" ....
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..
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....
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....
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
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 :-))
ASKER
i used to vs.net for both adapters.
then,i did this is in savebtn_click():
savebtn_click(){
SqlCommandBuilder cb = new SqlCommandBuilder(sqlDataA
SqlCommandBuilder cb2 = new SqlCommandBuilder(sqlDataA
sqlDataAdapter1 .Update(dataSet1,"table1")
sqlDataAdapter2 .Update(dataSet1,"table2")
}
*********************updat
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(
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,tab
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"