none
get databound object of bound tetxtbox.text RRS feed

  • Question

  • Hi

    By designer I bind several  Textbox.text to several datacolumns of different objects (datatables columns)

    Now during control validating I need to get the datacolumn OBJECT to wich each textbox is bound 

    By using this code ...

    Dim TableColumnName = sender.DataBindings("Text").BindingMemberInfo.BindingField

      .. I can get the Property Name of the DataColumn object  but I need the OBJECT to wich this property belongs ( in othe words I need the DataColumn)

    How to get it ?

    Friday, January 18, 2019 11:11 AM

Answers

  • Okay, what follows are the basics. I'm not doing the sender part as that is easy enough to cast to the proper type of control, most likely a TextBox, if something else cast accordingly.

    • The TextBox is named companyNameTextBox bound to a column named CompanyName. The data source is a BindingSource which is standard for working with a typed DataSet.
    • Drill down to the DataSource of the Binding, in this case a BindingSource.
    • From the above, cast the BindingSource.DataSource to a DataTable.
    • Access the specific column companyNameTextBox is bound too.
    • Query if the column is true or false for AllowDBNull.
    Dim ColumnName = companyNameTextBox.DataBindings("Text").BindingMemberInfo.BindingField
    Dim result = CType(CType(companyNameTextBox.DataBindings(0).
            DataSource, BindingSource).DataSource, DataTable).Columns(ColumnName).AllowDBNull

    To ensure this works, I loaded data then set CompanyName AllowDBNull to False, checked using the code above, works, next toggled AllowDBNull and checked again, correct result back from the code above.

    Now for you it's possible in the casting before the DataTable there is a hierarchy that is a DataSet so you have one additional cast.


    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

    • Marked as answer by Claudio111 Friday, January 18, 2019 6:34 PM
    Friday, January 18, 2019 12:24 PM
    Moderator

All replies

  • So let' say I have a DataTable with a field name of CompanyName, the DataTable becomes the DataSource of a BindingSource which leaves us with a TextBox data bound

    companyNameTextBox.DataBindings.Add("Text", bsCustomers, "CompanyName")
    What exactly are you looking for in this situation?


    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

    Friday, January 18, 2019 11:34 AM
    Moderator
  • May be I was not clear.

    The textbox binding is made by designer to a typized Dataset

    In particular the textbox bounds to a datatable column .

    In the thextbox validating I get the sender form the event handling

    By this

    Dim TableColumnName = sender.DataBindings("Text").BindingMemberInfo.BindingField

    I get the column name the textbox.text is bound to.

    Now I want access the AllowDbNull property of the same column so I need the column Object or the DataTable name 

    Friday, January 18, 2019 11:48 AM
  • It does not matter if you bind to via the designer or not, we still get to the same place. Give me a little time I'm just starting breakfast.

    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

    Friday, January 18, 2019 12:07 PM
    Moderator
  • Okay, what follows are the basics. I'm not doing the sender part as that is easy enough to cast to the proper type of control, most likely a TextBox, if something else cast accordingly.

    • The TextBox is named companyNameTextBox bound to a column named CompanyName. The data source is a BindingSource which is standard for working with a typed DataSet.
    • Drill down to the DataSource of the Binding, in this case a BindingSource.
    • From the above, cast the BindingSource.DataSource to a DataTable.
    • Access the specific column companyNameTextBox is bound too.
    • Query if the column is true or false for AllowDBNull.
    Dim ColumnName = companyNameTextBox.DataBindings("Text").BindingMemberInfo.BindingField
    Dim result = CType(CType(companyNameTextBox.DataBindings(0).
            DataSource, BindingSource).DataSource, DataTable).Columns(ColumnName).AllowDBNull

    To ensure this works, I loaded data then set CompanyName AllowDBNull to False, checked using the code above, works, next toggled AllowDBNull and checked again, correct result back from the code above.

    Now for you it's possible in the casting before the DataTable there is a hierarchy that is a DataSet so you have one additional cast.


    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

    • Marked as answer by Claudio111 Friday, January 18, 2019 6:34 PM
    Friday, January 18, 2019 12:24 PM
    Moderator
  • thank you Karen

    Have a good weekend

    Friday, January 18, 2019 6:34 PM