Question ado.net datagrid dataset

parry2k

New member
Joined
May 4, 2009
Messages
1
Programming Experience
Beginner
hello all,

i'm good in programming but not too familiar with ado.net. I'm trying to make a simple application using mssql server, ado.net, .net 3.5 and sp's

I have following question

1. created a table with 2 fields (id , name) id is pk
2. created sp to add, delete, update/insert record in table.
3. created a form and shown two text fields to show the data from table and a grid to show all the record on the same form. 2 buttons added on the form new and save
4. create sqlconnection to server, create data adapter and set the data adapter insert, select and update command alongwith parameters and filled the data source.
5. bound the grid runtime with datasource and table.
6. on cellclick of datagrid, i get the record from grid and show the value in text fields. even grid is sorted, it is working find, i found some function from internet to achieve it.
7. on click of new button, i clear the text fields and internally set a boolean variable for new record.
8. on click of save button, if boolean varaible is true then add new row to dataset table and accept the changes.
9. it adds the record in table and show it in grid.


till this point everything worked fine.

the issue i'm facing is when user select a record from grid and do the changes on text field, on click of save, it need to update the record.

as i have boolean variable which tell me if it is exisiting or new record but i'm unable to find out the way to update the record.

there is a way i can do it, if i know the current record number in table of data set. in this case i can set the value of table coloumsn of that row and accept changes

but i have no way to get the row of the table becuase if grid is sorted then i don't have correct correspoding record number in data table.

how i can achieve this functionality.

any idea?

thanks in advance,

p
 
Please post in the most appropriate forum for the topic of your thread, not the first one you come to. Moved.

You should be:

1. Populating a DataTable using a DataAdapter.
2. Binding the DataTable to a BindingSource.
3. Binding the BindingSource to the DataGridView.
4. Binding the BindingSource to the TextBoxes too. That way there's no need to transfer data between the grid and the text fields.
5. To add a new record just call AddNew on the BindingSource.
6. To save, first call EndEdit on the BindingSource to commit any changes, then call Update on the same DataAdapter you called Fill on to save the changes in the DataTable.
 
If you struggle with this, post your project in a zip file, WITHOUT the bin and obj folders, and someone will take a look

If youre after a longer tutorial on the topic, see the DW2 link in my signature (observing the panel in the top right of the page for any advices that are .net 3.5 specific) section on Saving Data
 

Latest posts

Back
Top