none
What is an easy way to populate a datagrid cell when a textbox entry changes?

    Frage

  • Hi all... using VB/VS 2010 and would appreciate any suggestions.

    This involves two windows forms.  The first form creates and retrieves data per the 2 sections of datagrid code below.  The second form has a simple textbox to input "Contact".  I would like to know how to dynamically update or refresh the information in the datagrid view when it changes in the textbox.  Later I will save it back the the xml file but I have this figured out already.  Thanks...

    Sub createGrids()
            'create Table1
            Dim dt1 As New DataTable("table1")
            dt1.Columns.Add("Department", GetType(String))
            dt1.Columns.Add("Contact", GetType(String))
            dt1.Columns.Add("Role", GetType(String))
            dt1.Columns.Add("Expertise", GetType(String))

            With DataGridView1
                .DataSource = dt1
                .AllowUserToAddRows = True : .AllowUserToDeleteRows = True
                .AllowUserToOrderColumns = False : .AllowUserToResizeRows = True
            End With

    Sub getteams()
            Dim dt2 As New DataTable
            dt2.ReadXml("teams.xml")
            DataGridView1.DataSource = dt2
        End Sub

    Donnerstag, 11. Oktober 2012 11:17

Antworten

  • Hi

    I forgot to mention a few things :

    1) A constructor is needed for the Form that contains the TextBox, if the other form (the one containing the DataGrid) will be instantiating the former form. If not, provide the TextBox form a Public Method that takes 1 parameter - a reference to the datagrid - and saves the reference. Then, from the datagrid form, first invoke this method so that the dg reference is saved. Then the Tb_Leave handler will work automatically.

    2) Obviously, you would also need a TextBox_Enter handler which would save the pre-content of the textbox, which is used for comparing with the post-content in the TextBox_Leave handler.

    Steven

    • Als Antwort markiert 8Terrapin4 Donnerstag, 18. Oktober 2012 19:29
    Freitag, 12. Oktober 2012 12:56

Alle Antworten

  • You need to do 2 things :

    1) In the form that has the textbox, create a constructor that accepts 1 parameter : a reference to the datagrid which is to be updated. Store this argument as a module-level var as follows :

           Private  m_dg   As DataGrid

           Public Sub fTextBoxFrm(ByVal dg As DataGrid)

               m_dg  =  dg

          End Sub

    2) Use the TextBox_Leave Event Handler to update the DataGrid   :

            Private Sub tb_Leave(...) Handles tb.Leave

                  ' Check if the tb's contents have changed

                   If  tb.Text <> tbPrev.Text Then

                       tbPrev.Text =  tb.Text

                       ' Update DataGrid

                   End If

           End Sub

    Steven

    Donnerstag, 11. Oktober 2012 13:35
  • hi terrapin

    it looks/sounds like you have 2 issues to solve.

    the first is the communication between the 2 forms. In OOP these 2 objects should know nothing about each other. Therefore raising an event is one option. See here for an example of how to do this - you'll want the sibling to sibling section.

    Although I would question using a separate form just to input a simple value to filter the table. The code required will complicate matters and may not be justified. Why not have the TextBox on the same form as the datagridview?

    The second issue is how to filter the dt2 populated from the xml file. There are a few ways to do this - LINQ or a DataView would likely be the most common. Then set the DataGridView datasource to either the LINQ query or the DataView


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    Donnerstag, 11. Oktober 2012 17:08
  • Hi all -

    Thanks for responding and aplogies for delay.  I will be testing things based on your recommendations.

    Regards.

    Freitag, 12. Oktober 2012 12:13
  • Hi

    I forgot to mention a few things :

    1) A constructor is needed for the Form that contains the TextBox, if the other form (the one containing the DataGrid) will be instantiating the former form. If not, provide the TextBox form a Public Method that takes 1 parameter - a reference to the datagrid - and saves the reference. Then, from the datagrid form, first invoke this method so that the dg reference is saved. Then the Tb_Leave handler will work automatically.

    2) Obviously, you would also need a TextBox_Enter handler which would save the pre-content of the textbox, which is used for comparing with the post-content in the TextBox_Leave handler.

    Steven

    • Als Antwort markiert 8Terrapin4 Donnerstag, 18. Oktober 2012 19:29
    Freitag, 12. Oktober 2012 12:56