Skip to content

Please Help! How do I update several rows of data from a Gridview all at once?

Jay

Asked Aug 7, 2006, 4:15 PM EDT1 answer
Share
LinkedInX
Hi,
I have a gridview with text boxes (description fields) and dropdown lists along with bound fields on a Web form. I am using an ObjectDataSource. I am able to easily populate the relavant fields based on my ObjectDataSource. I have a need to be able to permit users to edit the text boxes and change the values in the dropdown list and save all the changes to all rows to the database. I am not using GridView's inbuilt select / update / insert functions in other words, I dont want to save changes per record, instead want to be able to save changes once when the user clicks on a save button.

One thought I had was to create a collection of objects for each row, but that would be an expensive operation with performance issues (if the list is long).

I was wondering if anyone else had faced this issue and how you solved it.

Any help is appreciated.

Regards
Jay

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    I have update the dataset from datagridview but by using VB.net and not C#. I am giving the code below. I hope it will help you.

    I have added the data set to gridview manually and not by using DataSource method (i.e. dgv.DataSource = ds.Tables("EmpTable"))

    I have four CheckBox Columns (Research, Writing, Proofing, done) in datagridview.

    Following is the code to update database table from datagridview:

     Dim strsql As String

            Dim adap As New SqlDataAdapter

            strsql = "SELECT * from Module"

            dgModule.Columns.Clear()

            dsModule.Clear()

     

                con.Open() '-- Opening connection to sql Server
                adap = new SqlDataAdapter(strsql)

                adap.Fill(dsModule, "ModuleTable")


    Dim dt As DataTable = dsModule.Tables("Module") '--Module is the table you are adding to DataGridView

                Dim row() As DataRow

                For i As Integer = 0 To dsModule.Tables("ModuleTable").Rows.Count - 1 '--Rows in the dataset

                    expr = "ModuleID=" & dgModule.Item("moduleID", i).Value '--This will take ModuleID from datagrid to update the record

                    row = dt.Select(expr) '--it will select the rows for update on the base of ModuleID

                    Dim dr As DataRow

                    For Each dr In row

                    '--Following code will store the changes made in datagrid row in datarow
                       
    dr("Research") = dgModule.Item("research", i).Value

                        dr("Writing") = dgModule.Item("writing", i).Value

                        dr("Proofing") = dgModule.Item("proofing", i).Value

                        dr("Done") = dgModule.Item("done", i).Value

                    Next

                Next

                com = New SqlCommandBuilder(adap) '--adap is a DataAdapter object created to fill the dataset

                adap.Update(dsModule, "Module") '--It will update the database

    I hope it will help you.

    I am trying the above code using C#. If you want I will send it to you.

    0