Search code examples
vb.netdatagridviewtextbox

VB.NET Pass values from textbox to datagridview


I want to pass values of textboxes to a datagridview using below code.

  Private Sub txtDrCr_Leave(sender As Object, e As System.EventArgs) Handles txtDrCr.Leave

    Dim nR As Integer
    nR = TxtList.Rows.Add()
    TxtList.Item("stsno", nR).Value = nR + 1
    TxtList.Item("stAcctCode", nR).Value = txtAccountCode.Text
    TxtList.Item("stAcctName", nR).Value = txtAccountName.Text
    TxtList.Item("stBranch", nR).Value = txtBranchName.Text
    TxtList.Item("stDescription", nR).Value = txtDescription.Text
    TxtList.Item("stAmount", nR).Value = txtAmount.Text
    TxtList.Item("stDrCR", nR).Value = txtDrCr.Text
    cleartextboxes()
End Sub

This code add two lines in the gridview whenever I leave the txtDRCR. I have attached the snapshot as well. Please need kind help. Thanks.

ImageAttached


Solution

  • Please use below code.

        Dim nR As Integer =0;
        Dim Row As Integer =0;
        TxtList.Rows.Add()
        Row = TxtList.Rows.Count - 2;
    
        TxtList("stsno", Row ).Value = nR + 1
        TxtList("stAcctCode", Row ).Value = txtAccountCode.Text
        TxtList("stAcctName", Row ).Value = txtAccountName.Text
        TxtList("stBranch", Row ).Value = txtBranchName.Text
        TxtList("stDescription", Row ).Value = txtDescription.Text
        TxtList("stAmount", Row ).Value = txtAmount.Text
        TxtList("stDrCR", Row ).Value = txtDrCr.Text
        TxtList.Refresh()