locked
Using DataGridView

    Question

  • Using a DataGridView, I would like to give the user the possibility of correcting any data and after that, only using the Enter key to confirm the change and update the table.

    The question is ho to do this.

    Monday, July 09, 2012 8:01 PM

Answers

  • Hello MarcosLeite,

    Carmelo,

    thanks for the answer, but the suggested link doesn't work.

    try again http://msdn.microsoft.com/en-us/library/k0e26a9k(v=vs.100)

    strange, click on the link, reporting page not found add the closing parenthesis after 100 in the quick search box.

    Regards.


    Monday, July 09, 2012 9:52 PM
  • Here is a code sample using the ADO.Net SqlDataAdapter UpdateCommand which you can invoke on the Datagridview's KeyDown or KeyPress Event.  Check if the Return key (Enter Key same thing) was pressed.  If yes then call the procedure.  In this example I am using a local dataTable called tblSubDetail which underlies a datagridview control on the form.  The user edits some data on the datagridview then if you press the Enter/Return key the procedure is called and updates the table on the Sql Server.  The local dataTable is already updated because that gets updated when the user edits the field on the Datagridview.  I probably left out some essential details (which hopefully you could figure out) because there is a little more to this than this short little example.  But the example is to give you a look at what you need to do.  I also added a variety of dataTypes -- Int, Varchar, Date, Money types.

    -----------------------------------------------------------------------------------------------

    If e.KeyCode = Keys.Return Then FixSubDetail    '-- now call FixSubDetail

    Private Sub FixSubDetail(ByVal s1 As String)

    Dim conn1 As SqlConnection
    conn1 = New SqlConnection
    conn1.ConnectionString = "Data Source=YourSvr;Initial Catalog=Subscriber;Integrated Security=True"

    Dim daB As SqlDataAdapter
    daB = New SqlDataAdapter

    daB.UpdateCommand = New SqlCommand
    daB.UpdateCommand.Connection = conn1
    Try
       daS.UpdateCommand.Parameters.Clear()

       daS.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 4, "SubDetailID")
       If s1.Equals("RecordID") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.Int, 4, "RecordID")
       If s1.Equals("ListNo") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.VarChar, 2, "ListNo")
       If s1.Equals("FirstDate") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.DateTime, 8, "FirstDate")
       If s1.Equals("CurExpireDate") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.DateTime, 8, "CurExpireDate")
       If s1.Equals("CheckNo") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.VarChar, 100, "CheckNo")
       If s1.Equals("PaidCheckAmt") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.Money, 8, "PaidCheckAmt")
       daS.UpdateCommand.CommandText = "Update SubDetail Set " & s1 & " = @P Where SubDetailID = @ID"
       Application.DoEvents()
       daS.Update(dsMain, "tblSubDetail")

       Catch ex As Exception
           MessageBox.Show(ex.Message, "Problem at FixSubDetail", MessageBoxButtons.OK)
       End Try
    End Sub


    Rich P

    Tuesday, July 10, 2012 10:16 PM

All replies

  • Hello MarcosLeite,

    Using a DataGridView, I would like to give the user the possibility of correcting any data and after that, only using the Enter key to confirm the change and update the table.

    The question is ho to do this.

    then still have a DataBase? so I suggest you create an application where data allow the user to insert, edit and update data, see an example here.

    http://msdn.microsoft.com/en-us/library/k0e26a9k(v=vs.90)

    Regards.


    Monday, July 09, 2012 8:40 PM
  • Carmelo,

    thanks for the answer, but the suggested link doesn't work.

    Monday, July 09, 2012 9:41 PM
  • Hello MarcosLeite,

    Carmelo,

    thanks for the answer, but the suggested link doesn't work.

    try again http://msdn.microsoft.com/en-us/library/k0e26a9k(v=vs.100)

    strange, click on the link, reporting page not found add the closing parenthesis after 100 in the quick search box.

    Regards.


    Monday, July 09, 2012 9:52 PM
  • Here is a code sample using the ADO.Net SqlDataAdapter UpdateCommand which you can invoke on the Datagridview's KeyDown or KeyPress Event.  Check if the Return key (Enter Key same thing) was pressed.  If yes then call the procedure.  In this example I am using a local dataTable called tblSubDetail which underlies a datagridview control on the form.  The user edits some data on the datagridview then if you press the Enter/Return key the procedure is called and updates the table on the Sql Server.  The local dataTable is already updated because that gets updated when the user edits the field on the Datagridview.  I probably left out some essential details (which hopefully you could figure out) because there is a little more to this than this short little example.  But the example is to give you a look at what you need to do.  I also added a variety of dataTypes -- Int, Varchar, Date, Money types.

    -----------------------------------------------------------------------------------------------

    If e.KeyCode = Keys.Return Then FixSubDetail    '-- now call FixSubDetail

    Private Sub FixSubDetail(ByVal s1 As String)

    Dim conn1 As SqlConnection
    conn1 = New SqlConnection
    conn1.ConnectionString = "Data Source=YourSvr;Initial Catalog=Subscriber;Integrated Security=True"

    Dim daB As SqlDataAdapter
    daB = New SqlDataAdapter

    daB.UpdateCommand = New SqlCommand
    daB.UpdateCommand.Connection = conn1
    Try
       daS.UpdateCommand.Parameters.Clear()

       daS.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 4, "SubDetailID")
       If s1.Equals("RecordID") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.Int, 4, "RecordID")
       If s1.Equals("ListNo") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.VarChar, 2, "ListNo")
       If s1.Equals("FirstDate") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.DateTime, 8, "FirstDate")
       If s1.Equals("CurExpireDate") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.DateTime, 8, "CurExpireDate")
       If s1.Equals("CheckNo") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.VarChar, 100, "CheckNo")
       If s1.Equals("PaidCheckAmt") Then daS.UpdateCommand.Parameters.Add("@P", SqlDbType.Money, 8, "PaidCheckAmt")
       daS.UpdateCommand.CommandText = "Update SubDetail Set " & s1 & " = @P Where SubDetailID = @ID"
       Application.DoEvents()
       daS.Update(dsMain, "tblSubDetail")

       Catch ex As Exception
           MessageBox.Show(ex.Message, "Problem at FixSubDetail", MessageBoxButtons.OK)
       End Try
    End Sub


    Rich P

    Tuesday, July 10, 2012 10:16 PM