Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to insert, update, delete using DataGrid ?
 

How to insert, update, delete using DataGrid ?

Hi
I am develoing a C# and MS Access application in which I have to display table rows and columns in a DataGrid AND in the same form I have to perform insert, update, delete operations.

Displaying the table's contents is not a problem but I need help in implementing insert, update and delete operations.

Thanx.
Anurodh  Monday, October 10, 2005 10:09 PM

Hi,

Implementing a Delete, Insert and Update operations you must have a DataAdapter and define its Delete, Insert and Update commands. You could use this adapter against your datasource and call the adapters' update method...

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table1", conn);
adapter.InsertCommand = new SqlCommand("INSERT INTO table1 VALUES(@param1, @param2)", conn);
adapter.InsertCommand.Parameters.Add("@param1",...);

Just do the same to the Update and Delete commands. Then, Create a DataSet and Fill it using the adapter and make it as a datasource of your datagrid...

DataSet ds = new DataSet();
adapter.Fill(ds);
DataGrid1.DataSource = ds;

When you want to send your changes to the database you can just call the adapter.update method...

adapter.Update(ds);

 

cheers,

Paul June A. Domag

Paul Domag  Tuesday, October 11, 2005 1:08 AM

Hi,

Defaultly you can edit the values in the datagrid unless you disable the grid. So the problem here lies on the delete and add. In the add button you can directly add a newrow to your datasource and it will be reflected on your datagrid. Same also with delete:

// On your AddButton_Click event
   DataRow dr = dt.NewRow();
   dt.Rows.Add(dr);
   dataGrid1.CurrentRowIndex = dt.Rows.Count - 1;

// On your DeleteButton_Click event
   dt.Rows.RemoveAt(dataGrid1.CurrentRowIndex);

(Where dt is a datatable object)

 

cheers,

Paul June A. Domag

Paul Domag  Wednesday, October 12, 2005 1:02 AM

Hi,

Yes, the DataGridView is a new control that supercedes the old DataGrid. In DataGridView you can take advantage of your old code (the code posted) or try modifying the data directly on the DataGridView by accessing the Rows collection of the grid...

// add new
dataGrid1.Rows.Add();
// remove
dataGrid1.Rows.RemoveAt(<index>);

 

cheers,

Paul June A. Domag

Paul Domag  Friday, October 14, 2005 2:12 AM

Hi,

Implementing a Delete, Insert and Update operations you must have a DataAdapter and define its Delete, Insert and Update commands. You could use this adapter against your datasource and call the adapters' update method...

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table1", conn);
adapter.InsertCommand = new SqlCommand("INSERT INTO table1 VALUES(@param1, @param2)", conn);
adapter.InsertCommand.Parameters.Add("@param1",...);

Just do the same to the Update and Delete commands. Then, Create a DataSet and Fill it using the adapter and make it as a datasource of your datagrid...

DataSet ds = new DataSet();
adapter.Fill(ds);
DataGrid1.DataSource = ds;

When you want to send your changes to the database you can just call the adapter.update method...

adapter.Update(ds);

 

cheers,

Paul June A. Domag

Paul Domag  Tuesday, October 11, 2005 1:08 AM
Thats fine Paul but my problem is do this taking values from DataGrid.
I hv a form with one datagrid(showing one access table) and four buttons(one each for add, edit, delete and close the aplication).
On the click event of these buttons according to the operation performed, I hv to insert a ner row with values, same for update and delete.

I think now u can help me better.

Thanks.
Anurodh  Tuesday, October 11, 2005 10:28 PM

Hi,

Defaultly you can edit the values in the datagrid unless you disable the grid. So the problem here lies on the delete and add. In the add button you can directly add a newrow to your datasource and it will be reflected on your datagrid. Same also with delete:

// On your AddButton_Click event
   DataRow dr = dt.NewRow();
   dt.Rows.Add(dr);
   dataGrid1.CurrentRowIndex = dt.Rows.Count - 1;

// On your DeleteButton_Click event
   dt.Rows.RemoveAt(dataGrid1.CurrentRowIndex);

(Where dt is a datatable object)

 

cheers,

Paul June A. Domag

Paul Domag  Wednesday, October 12, 2005 1:02 AM
Thanks Paul!!!
Now, there is one new feature in Visual C# 2005, i.e. DataGridView hope u know that.
Plz tell me how to insert and delete in that on button_click events.

Thanx in advance.
Anurodh  Friday, October 14, 2005 1:37 AM

Hi,

Yes, the DataGridView is a new control that supercedes the old DataGrid. In DataGridView you can take advantage of your old code (the code posted) or try modifying the data directly on the DataGridView by accessing the Rows collection of the grid...

// add new
dataGrid1.Rows.Add();
// remove
dataGrid1.Rows.RemoveAt(<index>);

 

cheers,

Paul June A. Domag

Paul Domag  Friday, October 14, 2005 2:12 AM

hello there

do you know how do delete / update if i have 3 tables that connected?

it's goes like this:

Categories

     Category ID

     CategoryName

Products

     ProductID

     ProductName

     ProductPrice

     CategoryID

Product Details

     Part_id

     Part name

     description

     Product ID

i want to delete/delete a row from datagrid of the product table how do i do that?

remmeber: i have data in productDetails.

Please help if you can it is very very very important to me.

 

 

 

gadym  Friday, December 16, 2005 11:02 AM

HI paul,

Even thought i have got same problem with datagridview in VS 2005, but as per ur suggestion there is no such dataView1.rows.add()

or dataview1.rows.removeat(<index>)property.

so for that , will u tell me more ?

regards,

avinash.

avding  Sunday, July 30, 2006 11:15 AM

Hi avding,

Here's the documentation for the Datagrid.Rows property:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

Is it possible that your using the ASP.Net Datagridview control? Coz this documentation is for windows forms applications.

cheers,

Paul June A. Domag

Paul Domag  Monday, July 31, 2006 4:04 PM

Hi gadym,

To do this you must create a dataset which would contain your 3 datatables. After that create relationships between your datatables inside your dataset. Try using DataRelation to do that. Here's a MSDN documentation regarding the DataRelation Class.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarelationclasstopic.asp

cheers,

Paul June A. Domag

Paul Domag  Monday, July 31, 2006 4:13 PM

Hi Paul ,

u r correct that i am using ASP.NET apps and for that i am trying to find out same process.

even if possible then will u tell that how to configure dataGridView for SqlDataSource?

bcoz currently i am using sql-server 2000 and studio 2005 but it is getting problem while connecting using System.Data.SqlClient so if u have idea about both then please share with me.

regards,

avding.

avding  Tuesday, August 01, 2006 5:51 AM

Hi,

When you said, "getting problem while connecting using System.Data.SqlClient", what problem do you mean? Could you elaborate on this?

Also, you would have a greater chance on being answered in http://forums.asp.net/ for this forums does not focus on ASP.Net technology.

 

cheers,

Paul June A. Domag

Paul Domag  Friday, August 04, 2006 6:17 PM

acutally i got the solution on this problem...

as we are usring sqlSource to connect for the same in VS 2005.

~avding

avding  Friday, August 18, 2006 1:11 PM

hi,

will u please tell me how to find out the let and top values of a perticular cell from dataGridView .

Actually i want to put a combobox externally on to the datagrid, as i enter into a cell i can disply that combo box over that cell,

does anyone has this idea?

~Avinash

avding  Thursday, November 02, 2006 12:17 PM
ntsoo  Thursday, November 02, 2006 1:10 PM
Thanks for the url!!
Ashish_Salve_abc4d0  Sunday, February 18, 2007 8:36 AM
I got data from file.DBF, but I opened and read instead of using DataAdapter. Then I displayed records in DataGridView (using DataTable), but I just do not know how to update data back to database. Could you help me, please?
babychicken2  Tuesday, January 15, 2008 4:33 AM
hi paul,

explain datagrid update concept ..

in c#.net 2005

pls help me sir,,

m.vigneshbabu@gmail.com

send this codingin this mail..






vicky87  Monday, December 15, 2008 12:35 PM

Hi sir,
Sir, m a New bie in asp.net.i m designing a login page for which ijust wanted toask that how can i read the data of an sql table using data adapters which would further help a user to login. we just have to use 2 feilds of the table.


Thanks in Advance.

Kshitij12  Tuesday, August 04, 2009 6:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Saving DataGridView filtered data
• how to bind 2 columns to same lookup table with combobox?
• "Freeze" datagridview sorting and filtering
• Datagrid ColumnStyle MappingName To SubClass of a Collection
• link a ComboBox to another table in form
• CustomControls in DGV's
• bind 4 string arrays as columns to a datagrid
• Binding ProgressBar to Properties in other Threads
• Windows Forms, Data UI Customization Question
• Providing INotifyPropertyChanged