locked
How to insert, update, delete using DataGrid ? RRS feed

  • Question

  • 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.
    Monday, October 10, 2005 10:09 PM

Answers

  • 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

    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

    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

    Friday, October 14, 2005 2:12 AM

All replies

  • 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

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

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

    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.

     

     

     

    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.

    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

    • Proposed as answer by Major MIS Thursday, July 22, 2010 7:04 PM
    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

    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.

     

    Tuesday, August 1, 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

    Friday, August 4, 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

     

    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

     

    Thursday, November 2, 2006 12:17 PM
  • Thursday, November 2, 2006 1:10 PM
  • Thanks for the url!!  
    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?
    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 coding in  this mail..






    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 i just wanted to ask 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.

    Tuesday, August 4, 2009 6:25 AM
  • This code is not working in vb... I tried it, please check your code....modify it for the datagrid... add, update delete
    ShoaibA Shaikh
    Thursday, December 15, 2011 9:41 PM