none
TextBox bound to DataTable RRS feed

  • Question

  • Hi all

    I have a Win Form with several textboxs

    Each text box bounds to DataTable Column (runtime) with code like :

    ' get Column Name
    col = UIS_DT_Clienti.ClienteDataCreazioneColumn.ColumnName
    
    'create Binding object
            BindingClienteDataCreazione = New Binding("Text", UIS_DT_Clienti, col)
    
    ' set bound
     TxtClienteDataCreazione.DataBindings.Add(BindingClienteDataCreazione)

    With buttons I navigate thru DataTable and choose a current row getting all textboxs filled with data column value


    Now,  in the form I have a button to insert New Row in DataTable.

    The problem is that  in this case all TextBox should be Empty, but they are still filled with last current row.

    So How to get Textboxs with Empty values when  I need to insert new row ?

    Tuesday, November 20, 2018 9:57 AM

All replies

  • The reason why it has the current row is you have not move to the new row. In the example below which I had from many years ago, did not just create it show this.

    If I press the "Add" button in the BindingNavigator a new row is created and moves to it, press Button2 does the same. Press Button1 and we remain on the current row as you have now.

    Public Class Form1
        Private _bs As New BindingSource
        Private Sub b_Format(sender As Object, cevent As ConvertEventArgs)
            If cevent.DesiredType IsNot GetType(String) Then
                Exit Sub
            End If
            cevent.Value = CType(cevent.Value, Decimal).ToString("c")
        End Sub
        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 = "C1", .DataType = GetType(Decimal)})
            dt.Rows.Add(New Object() {199999})
            dt.Rows.Add(New Object() {20})
            dt.Rows.Add(New Object() {10})
    
            _bs.DataSource = dt
            BindingNavigator1.BindingSource = _bs
    
    
            Dim b As Binding = New Binding("Text", _bs, "C1")
    
            AddHandler b.Format,
                Sub(s As Object, args As ConvertEventArgs)
                    If Not String.IsNullOrWhiteSpace(args.Value.ToString) Then
                        If Not IsDBNull(args.Value) Then
                            args.Value = Convert.ToInt32(args.Value).ToString("C2")
                        Else
                            args.Value = "N/A" ' whatever makes sense
                        End If
                    Else
                        args.Value = "N/A" ' whatever makes sense
                    End If
    
                End Sub
    
            AddHandler b.Parse,
                Sub(s As Object, args As ConvertEventArgs)
                    If args.DesiredType IsNot GetType(Decimal) Then
                        Exit Sub
                    End If
    
                    If Not String.IsNullOrWhiteSpace(args.Value.ToString) Then
                        args.Value = Decimal.Parse(args.Value.ToString, Globalization.NumberStyles.Currency, Nothing)
                    Else
                        args.Value = 0
                    End If
    
    
                End Sub
    
            UnitPriceTextBox.DataBindings.Add(b)
    
        End Sub
        ''' <summary>
        ''' If you simply add the row and not move to it the current row
        ''' remains current while using MoveLast we are now on the new row
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(_bs.DataSource, DataTable).Rows.Add()
            _bs.MoveLast()
        End Sub
        ''' <summary>
        ''' Add new row and move to the new row
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            _bs.AddNew()
        End Sub
    End Class

    Here N/A appears because of the Format event logic


    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


    Tuesday, November 20, 2018 10:59 AM
    Moderator
  • Hi,

    I agree with karen's approach to solving your problem,but you need to know two events below. 

    Binding.Format Event

    Binding.Parse Event

    Best Regards,

    Alex


    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.

    Wednesday, November 21, 2018 3:05 AM