dgv and a sub?

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
I have my form working, but I am reusing a lot of code to do updates and edits to the data. I want to localize these and make them into their own subs that I can call with in my other subs.

Originally I was using a sub with the System.Windows.Forms.DataGridViewCellEventArgs event. Now that I am doing a new sub, I do not want to use this, as it is nearly impossible to supply the correct arguments for that event.

how do I need to call to make a call to the following sub work properly?

I was trying to call something like insertDB(DataGridView1.CurrentRowIndex + 1), but of course that is not going to give me what I need. By the way, this sub is what I am using to create a new record on a btn_click handler.

VB.NET:
Private Sub insertDB(ByVal rowIndex As Integer)

    _Conn = New SqlConnection(_ConnString)
    Try
      Dim theRow As Integer = rowIndex
      Dim gridRow As DataGridViewRow = DataGridView1.Rows(theRow)

      Dim theDate As Date = Date.Parse(DateTimePicker1.Text)
      Dim depositID As Integer = 0

      Dim strHVAC As String = ""
      strHVAC = gridRow.Cells(1).EditedFormattedValue.ToString().Replace("$", "").Replace(",", "")
      Dim dblHVAC As Double = 0.0
      If strHVAC.Length > 0 Then dblHVAC = Double.Parse(strHVAC)

      Dim strPlumbing As String = ""
      strPlumbing = gridRow.Cells(2).EditedFormattedValue.ToString().Replace("$", "").Replace(",", "")
      Dim dblPlumbing As Double = 0.0
      If strPlumbing.Length > 0 Then dblPlumbing = Double.Parse(strPlumbing)

      Dim isBidJob As Boolean = Boolean.Parse(gridRow.Cells(3).EditedFormattedValue.ToString())
      Dim isMisc As Boolean = Boolean.Parse(gridRow.Cells(4).EditedFormattedValue.ToString())
      Dim strNote As String = gridRow.Cells(5).EditedFormattedValue.ToString()
      Dim strName As String = gridRow.Cells(6).EditedFormattedValue.ToString()
      Dim strCheckNumber As String = gridRow.Cells(7).EditedFormattedValue.ToString()
      Dim isCOD As Boolean = Boolean.Parse(gridRow.Cells(8).EditedFormattedValue.ToString())

      'Dim scb As New SqlCommandBuilder(_da)
      '_da.UpdateCommand = scb.GetUpdateCommand()
      '_da.InsertCommand = scb.GetInsertCommand()
      '_da.DeleteCommand = scb.GetDeleteCommand()

      Dim InsCommandString As String = "usp_Deposit_Create"


      Dim cmdINS As New SqlCommand(InsCommandString, _Conn)

      cmdINS.CommandType = CommandType.StoredProcedure

      Dim myUtil As New Utils
      With myUtil
        .AddParamToSQLCmd(cmdINS, "@DepositID", SqlDbType.Int, 4, ParameterDirection.Output, 0)
        .AddParamToSQLCmd(cmdINS, "@HVAC", SqlDbType.Money, 8, ParameterDirection.Input, dblHVAC)
        .AddParamToSQLCmd(cmdINS, "@PLUMBING", SqlDbType.Money, 8, ParameterDirection.Input, dblPlumbing)
        .AddParamToSQLCmd(cmdINS, "@IsBidJob", SqlDbType.Bit, 1, ParameterDirection.Input, IIf(isBidJob, 1, 0))
        .AddParamToSQLCmd(cmdINS, "@DateDeposited", SqlDbType.DateTime, 8, ParameterDirection.Input, theDate.ToShortDateString())
        .AddParamToSQLCmd(cmdINS, "@MISC", SqlDbType.Bit, 1, ParameterDirection.Input, IIf(isMisc, 1, 0))
        .AddParamToSQLCmd(cmdINS, "@Notes", SqlDbType.NVarChar, 1000, ParameterDirection.Input, strNote)
        .AddParamToSQLCmd(cmdINS, "@Name", SqlDbType.NVarChar, 35, ParameterDirection.Input, strName)
        .AddParamToSQLCmd(cmdINS, "@CheckNumber", SqlDbType.NVarChar, 15, ParameterDirection.Input, strCheckNumber)
        .AddParamToSQLCmd(cmdINS, "@FromInvoices", SqlDbType.Bit, 1, ParameterDirection.Input, IIf(isCOD, 1, 0))
      End With

      _da.InsertCommand = cmdINS

      _Conn.Open()
      cmdINS.ExecuteNonQuery()
      _Conn.Close()
    Catch ex As Exception
      MessageBox.Show("ERROR in insertDB(): " & ex.Message & vbCrLf & vbCrLf & ex.StackTrace)
    Finally
      _Conn.Dispose()
    End Try
    calcTotals()
    updateBS()

  End Sub
 
You shouldn't be doing any of that at all. Just put your initial data into a DataTable and bind that to the grid. Any additions, changes or deletions will be automatically propagated to the DataTable, so then you simply pass that table to your adapter's Update method.
 

Latest posts

Back
Top