Resolved How to update selected row in datagridview through textbox located in another form?

Post number 2 has been selected as best answer.

rajdh75

Member
Joined
Mar 30, 2020
Messages
6
Programming Experience
Beginner
Hello,
I am using vb.net and access database.
I have FrmInvoice which has a unbound datagridview1.
It has columns like ProductID, Product Name, Qty, Rate, Amount.
I have a second form named as FrmAddItems for transferring values to FrmInvoice's datagridview through Textboxes
ie. TxtProductID, TxtProductName, TxtQty, TxtRate, TxtAmount.

The datagridview1's property - modifiers is set to public.
I am adding rows from FrmAddItems. The code is -
FrmInvoice.DataGridView1.Rows.Add(TxtProductID, TxtProductName, TxtQty, TxtRate, TxtAmount).
This code works fine.
FrmAddItems has Add, Update, delete button in it.

I am getting values in FrmAddItems's Text boxes from DataGridView1 cellcontentclick event, its code is

VB.NET:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name

With FrmAddItems
FrmAddItems.TxtProductID = DataGridView1.CurrentRow.Cells(0).Value
FrmAddItems.TxtProductName = DataGridView1.CurrentRow.Cells(1).Value
FrmAddItems.TxtQty.Text = DataGridView1.CurrentRow.Cells(2).Value
FrmAddItems.TxtRate.Text = DataGridView1.CurrentRow.Cells(3).Value
FrmAddItems.TxtAmount.Text = DataGridView1.CurrentRow.Cells(4).Value

FrmAddItems.Show()
FrmAddItems.BtnAdd.Visible = False
End With

End Sub
This code also works fine.

I want to update selected row from this FrmAddItems when update button is pressed.
can anyone help on this ?
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
For a start, you wouldn't call Show. You would call ShowDialog to display the form as a modal dialogue. What you should be doing is creating the form, passing in the required data and calling ShowDialog. When ShowDialog returns you know the dialogue has closed. If needs be, you can determine the result of the action in the dialogue by the value returned by ShowDialog. You can then get data back from the dialogue as required and update the current form. If you were only using the dialogue for editing and based on your current code, that might look something like this:
VB.NET:
Dim cells = DataGridView1.Rows(e.RowIndex).Cells

Using dialogue As New FrmAddItems
    With dialogue
        .TxtProductID.Text = cells(0).Value.ToString()
        .TxtProductName.Text = CStr(cells(1).Value)
        .TxtQty.Text = cells(2).Value.ToString()
        .TxtRate.Text = cells(3).Value.ToString()
        .TxtAmount.Text = cells(4).Value.ToString()
        
        .BtnAdd.Visible = False
        
        If .ShowDialog() = DialogResult.OK Then
            cells(0).Value = Convert.ToInt32(.TxtProductID.Text)
            cells(1).Value = .TxtProductName.Text
            cells(2).Value = Convert.ToInt32(.TxtQty.Text)
            cells(3).Value = Convert.ToDecimal(.TxtRate.Text)
            cells(4).Value = Convert.ToDecimal(.TxtAmount.Text)
        End If
    End With
End Using
In the dialogue form, you simply set the DialogResult property of the form and it will close and return that value from ShowDialog.
 

rajdh75

Member
Joined
Mar 30, 2020
Messages
6
Programming Experience
Beginner
Thank you very much for your solution.
It works fine.
 
Top Bottom