none
data grid view last row value clear text boxes VB.net RRS feed

  • Question

  • I have a data grid view and number text boxes and other UI controls on windows form. The data grid view has data gridview selection changed event.when the user selects data grid view and click approve button the data related to that row moves to next form.This way all the rows are cleared from data gridview with approve button. However on last row, when the user clicks it the corresponding values are displayed in text boxes and on click of the approve button the data grid view becomes empty but the text boxes still contains last row(datagrid view) values. How can we clear these test boxes. I tried clearing the text boxes with txtbox.text="" but still data retains in text box.
    Thursday, November 9, 2017 5:00 PM

All replies

  • Hi

    You will need to show the code you are using in order to get some help.

    You will also need to explain where the data in the DataGridView is coming from - is a DataTable bound to it - or something else?


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 9, 2017 5:16 PM
    Thursday, November 9, 2017 5:15 PM
  • We need to know for instance, did you use data wizards to setup data controls on your form? This would mean there is a TableAdapter and a BindingSource in design view. If so then everything is data bound between a DataSet, BindingSource and controls including the DataGridView. If this is the case then you could on the last row use MyBindingSource.RemoveCurrent which should clear the TextBox controls and if not if all done working data you could clear the DataBindings on the TextBoxes.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 9, 2017 6:10 PM
    Moderator
  • Yes, data table is bound to the datgridview.

    Dim dt as datatable

    datagridview.datasource=dt

    Thursday, November 9, 2017 6:44 PM
  • Yes, data table is bound to the datgridview.

    Dim dt as datatable

    datagridview.datasource=dt


    So did you data bind the TextBoxes e.g. txtSomeBox.DataBinding.Add("Text",dt,"SomeField").  If so then clear the bindings. Otherwise there is nothing to stop you from using tstSomeBox.Text = "".

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 9, 2017 6:52 PM
    Moderator
  • Yes, data table is bound to the datgridview.

    Dim dt as datatable

    datagridview.datasource=dt

    Hi

    Don't know if this helps,but this is a test project to show removal of all rows one by one as button is clicked.

    ' Form1 with blank DataGridView1 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim dt As New DataTable("Freddy")
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With dt
                .Columns.Add("One", GetType(Integer))
                .Columns.Add("Two", GetType(String))
                .Columns.Add("Three", GetType(Decimal))
                .Columns.Add("Four", GetType(Boolean))
    
                ' some dummy data
                .Rows.Add(1, "One", 1.2D, True)
                .Rows.Add(2, "Two", 2.2D, False)
                .Rows.Add(3, "Three", 3.2D, True)
                .Rows.Add(4, "Four", 4.2D, False)
            End With
            With DataGridView1
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .MultiSelect = False
                .DataSource = dt
            End With
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If DataGridView1.SelectedRows().Count < 1 Then Exit Sub
            Dim r As DataGridViewRow = DataGridView1.SelectedRows(0)
            If Not r.Index = DataGridView1.NewRowIndex Then
                DataGridView1.Rows.Remove(r)
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 9, 2017 7:07 PM
    Thursday, November 9, 2017 7:06 PM
  • So here is an example that will data binding to a field and show data in a DataGridView.

    Public Class Form1
        Private bsData As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn With {.ColumnName = "Check", .DataType = GetType(Boolean)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "FirstName", .DataType = GetType(String)})
    
            dt.Rows.Add(New Object() {False, "Karen"})
            dt.Rows.Add(New Object() {False, "Kara"})
            dt.Rows.Add(New Object() {False, "Kim"})
            dt.Rows.Add(New Object() {False, "Jim"})
            bsData.DataSource = dt
            TextBox1.DataBindings.Add("Text", bsData, "FirstName")
            DataGridView1.DataSource = bsData
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            bsData.RemoveCurrent()
        End Sub
    End Class
    Press button1 to remove the current row, when no rows remain the TextBox will be empty


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 9, 2017 8:51 PM
    Moderator
  • Thanks karen and Les for the code but for some reason it didnt worked for me. However i tried clearing all controls from the group box with the following code and it worked.

     For Each ctr As Control In gbCoreData.Controls
                If TypeOf ctr Is TextBox Then
                    ctr.Text = ""
                ElseIf TypeOf ctr Is CheckedListBox Then
                    Dim clb As CheckedListBox = DirectCast(ctr, CheckedListBox)
                    For Each checkedItemIndex As Integer In clb.CheckedIndices
                        clb.SetItemChecked(checkedItemIndex, False)
                    Next
                ElseIf TypeOf ctr Is CheckBox Then
                    DirectCast(ctr, CheckBox).Checked = False
                ElseIf TypeOf ctr Is ComboBox Then
                    DirectCast(ctr, ComboBox).SelectedIndex = 0
                End If
            Next

    Sunday, November 12, 2017 7:01 PM
  • Hi dev.nets,

    Glad to hear this issue has been solved by yourself. Thanks for your sharing, I will introduce this experience to other forum users who face the same condition. Please mark your reply to close this case.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 20, 2017 8:13 AM
    Moderator