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.


Answer this question

How to insert, update, delete using DataGrid ?

  • pierguido

    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



  • Yahor Sinkevich

    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.

     

     

     


  • psy_ill

    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



  • HimaBinduVejella

    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



  • Ariadne22

    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



  • Kevin Gordon

    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



  • Albinowookie

  • Ignacio Danta

    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.



  • MicSup

    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



  • Jason Timms

    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.

  • noiile

    acutally i got the solution on this problem...

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

    ~avding



  • Timothy John Peters

    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.



  • Brandon Hamm

    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.

  • bfaulk1

    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



  • How to insert, update, delete using DataGrid ?