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

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
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
rich
technicalganesh
rich
Imports
System.DataImports System.Data.SqlClient
Partial
Class cdo_edit_resultsInherits 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
End Sub Protected Sub dgTests_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgTests.PageIndexChangingds.DataBind()
dgTests.DataSource = ds
dgTests.DataBind()
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.RowEditingdgTests.EditIndex = e.NewEditIndex
End Sub Protected Sub dgTests_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgTests.RowUpdating Dim results As TextBoxDataBind()
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 SubEnd
Classcarolyn 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
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()
End Subds.DataBind()
BindData()
_Matze_
thanks
rich
DevGuru22
thanks
rich
Imports
System.DataImports System.Data.SqlClient
Partial
Class cdo_edit_resultsInherits 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 ThenBindData()
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.PageIndexChangingdgTests.PageIndex = e.NewPageIndex
End Sub Protected Sub dgTests_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles dgTests.RowEditingBindData()
dgTests.EditIndex = e.NewEditIndex
End Sub Protected Sub dgTests_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgTests.RowUpdatingBindData()
myDataAdapter.Update(myDataSet)
End SubdgTests.EditIndex = -1
BindData()
End
Class