How to update a row(s) content in a datagrid

Hi everyone, 
 
  I am using vs 2005 vb/asp .net and I have a datagrid that I need to allow the users the ability to update rows. I have the grid at the point where you click edit on a row and it gives you the text boxes. Once they click update I don't know how to get that value and save it to the DB. Any help would be greatly appreciated...

thanks
rich



Answer this question

How to update a row(s) content in a datagrid

  • EltonH

    Hi,

    What datasource are you setting the datagrid to If your datasource is a DataTable then you'll have to issue an update from the dataadapter.

    myAdapter.Update(myDatatable)

    Given the fact that you have already defined your INSERT, UPDATE and DELETE commands...

     

    cheers,

    Paul June A. Domag



  • SirErugor

    Hi,

    Basing on your code snippet, it seems that you didn't specified the INSERT, UPDATE and DELETE commands of the dataadapter.

    ex.

    myAdapter.InsertCommand = new SqlCommand("INSERT INTO....");
    myAdapter.UpdateCommand = new SqlCommand("UPDATE....");
    myAdapter.DeleteCommand = new SqlCommand("DELETE....");

    Just search the docs for extra information...

     

    cheers,

    Paul June A. Domag



  • Blessed Geek

    Once you have data in your Dataset, why dont you do:
    myDataAdaptor.Update(datasetName)

    It will update all the dirty rows to the DB.

  • BA2006

    Hi,

    Try deleting it directly in your dataset. Just get the table and remove the rows manually. (Note:this action wouldn't delete the data in the database)...

     

    cheers,

    Paul June A. Domag



  • Ende

    Ok I tried to place them in different places with in the code but I still can not get it to work... :(  Do you have a simple example of something using this DataAdapter - I never work with this before I always used DataReader for my projects...

    thanks
    rich


  • Kris R

    Thank you - I will read through it....

    rich

  • technicalganesh

    I FIGURED IT OUT... For all those that need help! See the code below - I tested it and it works perfectly... thanks to all those that tried helping...

    rich

    Imports System.Data
    Imports System.Data.SqlClient

    Partial Class cdo_edit_results
       Inherits System.Web.UI.UserControl

    Dim myConnection As New SqlConnection(ConfigurationManager.AppSettings("conn"))
    Dim myDataAdapter As New SqlDataAdapter
    Dim strSQL As String
    Dim ds As New SqlDataSource

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
       BindData()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
       BindData()
    End If

    End Sub

    Protected Sub BindData()

    ds.ConnectionString = ConfigurationManager.AppSettings("conn")

    strSQL = "SELECT T.test_function, T.result_type, T.test_num, T.test_descr, T.test_function, T.ID, R.result, R.pass, R.cushion, R.limit " _
    &
    "FROM CDO_Quality_Test T INNER JOIN CDO_Quality_Results R ON T.ID = R.CDO_QT_ID " _
    &
    "WHERE R.Scenario_ID = " & Session("ScenarioID").ToString & " AND T.allowmodify = '1' " _
    &
    "ORDER BY T.Sub_test_pos"

    ds.SelectCommand = strSQL
    ds.DataBind()
    dgTests.DataSource = ds
    dgTests.DataBind()

    End Sub

    Protected Sub dgTests_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgTests.PageIndexChanging

    dgTests.PageIndex = e.NewPageIndex
    dgTests.SelectedIndex = -1

    BindData()

    End Sub

    Protected Sub dgTests_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles dgTests.RowEditing

    dgTests.EditIndex = e.NewEditIndex
    DataBind()

    End Sub

    Protected Sub dgTests_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgTests.RowUpdating

    Dim results As TextBox

    results = CType(dgTests.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)

    ds.UpdateParameters.Add("result", results.Text)

    ds.UpdateCommand =
    "UPDATE CDO_Quality_Results SET Result = @Result WHERE Scenario_ID = " & Session("scenarioid").ToString & " AND CDO_QT_ID = " &

    dgTests.DataKeys.Item(e.RowIndex).Value.ToString
    ds.Update()
    ds.DataBind()

    dgTests.EditIndex = -1

    BindData()

    End Sub

    End Class


  • carolyn H

    Hi,

    To understand the concepts, here is  a tutorial from MSDN. http://msdn2.microsoft.com/library/ms171932(en-us,vs.80).aspx

    Its a good place to start...

     

     

    cheers,

    Paul June A. Domag



  • toebens

    I can't see this being so hard... Does anyway have a simple vb. net example on how to update a row using a gridview I tried so many things and I've exhusted my efforts. I even bought a book for 45.00 and still have no dang luck. Here is the sample code I have now after playing for 2 weeks. I get it to update but now its updating every row in the db.

    Someone please help - it will be greatly appreciated....

    thanks
    rich

    Protected Sub BindData()

    ds.ConnectionString = ConfigurationManager.AppSettings("conn")

    strSQL = "SELECT T.test_function, T.result_type, T.test_num, T.test_descr, T.test_function, T.ID, R.result, R.pass, R.cushion, R.limit " _
    &
    "FROM CDO_Quality_Test T INNER JOIN CDO_Quality_Results R ON T.ID = R.CDO_QT_ID " _
    &
    "WHERE R.Scenario_ID = " & Session("ScenarioID").ToString & " AND T.allowmodify = '1' " _
    &
    "ORDER BY T.Sub_test_pos"

    ds.UpdateCommand =
    "UPDATE CDO_Quality_Results SET Result = @pResult WHERE Scenario_ID = " & Session("scenarioid").ToString

    ds.SelectCommand = strSQL
    ds.DataBind()

    dgTests.DataSource = ds
    dgTests.DataBind()

    End Sub

    Protected Sub dgTests_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgTests.RowUpdating

    ds.UpdateParameters.Add("pResult", "result") ' pResult = parameter and result = Database field from grid.

    ds.Update()
    ds.DataBind()
    BindData()

    End Sub


  • _Matze_

    I was using a dataset for my datasource originally however to write my insert I am calling a Datareader.executeNonQuery.

    thanks
    rich

  • DevGuru22

    Here is my code - can someone please help me - it's not working and I can't find where.

    thanks
    rich

    Imports System.Data
    Imports System.Data.SqlClient

    Partial Class cdo_edit_results
    Inherits System.Web.UI.UserControl

    Dim myConnection As New SqlConnection(ConfigurationManager.AppSettings("conn"))
    Dim myDataAdapter As SqlDataAdapter
    Dim strSQL As String
    Dim myDataSet As New DataSet

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    BindData()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Page.IsPostBack Then
       BindData()
    End If

    End Sub

    Protected Sub BindData()

    myConnection.Open()

    strSQL = "SELECT T.test_num, T.test_descr, T.test_function, R.result, R.pass, R.cushion, R.limit " _

    & "FROM CDO_Quality_Test T INNER JOIN CDO_Quality_Results R ON T.ID = R.CDO_QT_ID " _

    & "WHERE R.Scenario_ID = " & Session("ScenarioID").ToString & " AND T.allowmodify = '1' " _

    & "ORDER BY T.Sub_test_pos"

    myDataAdapter = New SqlDataAdapter(strSQL, myConnection)
    myDataAdapter.Fill(myDataSet)

    dgTests.DataSource = myDataSet
    dgTests.DataBind()

    myConnection.Close()

    End Sub

    Protected Sub dgTests_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgTests.PageIndexChanging

    dgTests.PageIndex = e.NewPageIndex
    BindData()

    End Sub

    Protected Sub dgTests_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles dgTests.RowEditing

    dgTests.EditIndex = e.NewEditIndex
    BindData()

    End Sub

    Protected Sub dgTests_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgTests.RowUpdating

    myDataAdapter.Update(myDataSet)
    dgTests.EditIndex = -1
    BindData()

    End Sub

    End Class


  • How to update a row(s) content in a datagrid