Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently working with a datagridview on a windows application. I have two specific columns which will work alongside each other. For simplicity, column 1 is used to select a license number, column 2 is used to select a licensed name (both of these columns are combo-box columns). When the end-user selects a name, I'm loading the license number combo-box with a data-set according to the ID of the person selected, for that particular row. This will load any license number(s) assigned to that specific person. While testing my code, the data-set loads correctly according to the person selected. Issue: The datagridview wants to load every row in column 1 with that specific data-set. I would like to have this data-set load column 1 only for the specific row number which the end-user is currently working with.

My code I'm currently testing with:
VB
Private Sub grdvwDeposit_CellValueChanged(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
      Handles grdvwDeposit.CellValueChanged

    If IsComboBoxCell(e) Then
      If e.ColumnIndex = 2 Then
          Call SetLicenseComboBoxes(e)
      End If
    End If

End Sub

Private Sub SetLicenseComboBoxes(ByVal e As DataGridViewCellEventArgs)

    Dim intVal As Integer
    Dim objWork As Object = Nothing

    Dim cur_cell As DataGridViewCell = Me.grdvwDeposit.CurrentCell
    Dim row As Integer = cur_cell.RowIndex
    Dim col As Integer = cur_cell.ColumnIndex

        If Me.grdvwDeposit.CurrentCell.ColumnIndex = 2 Then
            objWork = Me.grdvwDeposit.Rows(row).Cells(col).Value
            
            If IsNumeric(objWork) Then
                intVal = CInt(objWork)
            Else
                Exit Sub
            End If
            
            Dim intRow As Integer = row
            Do Until intRow = row - 1
                Call FillDataSet(mdsLicenseSpecific,
                        "usp_tblTransaction_Grid_License_FillComboBox_Specific",
                        "@ID_Person", intVal)
                Try
                    Call FillMassTransactionGridViewComboBoxFromDataset
                         (Me.grdvwDeposit.Rows(row),
                          Me.grdvwDeposit.Columns(1),
                          mdsLicenseSpecific)
                Catch ex As Exception
                          MessageBox.Show(ex.ToString, gstrAppName,
                          MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Try
             intRow -= 1
            Loop
            Exit Sub
        End If

End Sub



In my 'Do-Until' code, you can see that I'm loading a specific row and column, according to the current row being used. Any suggestions would be greatly appreciated.


Thanks,

GoBrvs01
Posted
Updated 9-May-12 8:08am
v4
Comments
Maciej Los 9-May-12 3:50am
   
On which event for DataGridView you call SetLicenseComboBoxes(ByVal e As DataGridViewCellEventArgs) procedure?
Maciej Los 9-May-12 13:10pm
   
Always, use "Improve question" button to upgrade your question.

1 solution

In my opinion Do...Loop loop in your code is unneccessary.

On my form i've placed DataGridView1 with columns:
Price - DataGridViewTextBoxColumn
TotalPrice - DataGridViewTextBoxColumn
DataGridView1 allow to edit cells.

Below code shows how to update TotalPrice in a specific row:
VB
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    'cell in a first column was updated?
    If e.ColumnIndex = 0 Then UpdateTotalPrice(e.RowIndex)
End Sub

Sub UpdateTotalPrice(ByVal iRow As Integer)
    'if no data ;)
    If iRow = -1 Then Exit Sub
    Dim price As Double = Me.DataGridView1.Rows(iRow).Cells("Price").Value
    Me.DataGridView1.Rows(iRow).Cells("TotalPrice").Value = price * 1.22 '+ 22%
End Sub
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900