Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to determine the field a textbox is bound to
Message
De
30/10/2010 18:39:07
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 8.0
OS:
Vista
Network:
Windows XP
Database:
Jet/Access Engine
Application:
Desktop
Divers
Thread ID:
01487443
Message ID:
01487659
Vues:
33
Well, glad you finally got it all ironed out. You're right ... one step at a time! <g>

~~Bonnie




>Bonnie,
>
>You're absolutely right.
>
>
>        Dim B As Binding = Me.DataBindings.Item(0)
>        Dim c As DataColumn = B.DataSource.datasource.tables(B.DataSource.datamember).columns(B.BindingMemberInfo.BindingField)
>
>
>works.
>
>A bit worried about what is going to happen when the dataset will contain more than one table because datamember is set at the formlevel, so how is that going to work when I implement datagrids?
>
>But I'll take it one issue at a time.
>
>Kind regards,
>
>Marc
>
>
>>>I puzzled. Here's what I read in MyForm.designer.vb:
>>>
>>>
>>>   me.MyTextbox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyFormBindingSource, "MyField", True))
>>>
>>>
>>>I would guess that somehow puts me in the first version of the syntax no?

>>
>>Yes and no. You're actually bound to a BindingSource, rather than directly to the DataTable. So now what you need to look at is where does the MyFormBindingSource get set and what is its .DataSource property set to ... has it been set to a DataTable or a DataSet (if the .DataSource property is a DataSet, then there will also be a MyFormBindingSource.DataMember property that will be set to the DataTable's name. Adjust your routines accordingly.
>>
>>~~Bonnie
>>
>>
>>>
>>>>
>>>>MyTextBox.DataBindings.Add("Text", MyDataSet.Tables["MyTable"], "MyBoundColumn");
>>>>
>>>
>>>You should not worry too much about your "dense-ity" today. You do understand my question even though, my explanations are somewhat foggy (yeah right, somewhat?). As they say in French, "what is understood thoroughly, can be expressed clearly and the words to say it come easily" (Boileau, my translation). I'm clearly not there yet. :)
>>>
>>>Thanks again and kind regards,
>>>
>>>Marc
>>>
>>>PS : and then in the series "is it dataset, is it a datareader, is it a datatable, is it a plain, is it a bird", it is a .... bindingsource (the type returned by my oBinding.datasource). Continuing my journey in .net land.
>>>
>>>>Marc,
>>>>
>>>>My guess is you're using the other binding syntax (there two different binding syntax you can use for databinding). Not a big deal, just need to look at it differently. See my blog post about this:
>>>>
>>>>http://geek-goddess-bonnie.blogspot.com/2009/09/keeping-datagrids-and-other-ui-controls.html
>>>>
>>>>I had assumed your TextBox was databound like this:
>>>>
>>>>
>>>>MyTextBox.DataBindings.Add("Text", MyDataSet.Tables["MyTable"], "MyBoundColumn");
>>>>
>>>>
>>>>When, in reality, it's bound like this:
>>>>
>>>>
>>>>MyTextBox.DataBindings.Add("Text", MyDataSet, "MyTable.MyBoundColumn");
>>>>
>>>>
>>>>Consequently, you'd need an additional line of code to get the DataTable's Name:
>>>>
>>>>
>>>>string TableName = oBinding.BindingMemberInfo.BindingPath;
>>>>
>>>>
>>>>Then you can check whether your DataSource is a DataSet and if so, get the TableName as I did above and proceed. If it's not, then you already have the DataTable.
>>>>
>>>>Your other question was about what to do if the DataSource is DataView rather than a DataTable. The answer is nothing ... at least as far as getting the column names. They'd be the same, whether they're in the DataTable or a DataView from that Table.
>>>>
>>>>Now, all that said ... half of what I've been talking about probably has nothing to do with your original question, which was: >Is there a way for a textbox to let it's findform produce these (dataset.datatable).cols?
>>>>
>>>>And I still quite understand what you're asking (sorry, I must be dense today).
>>>>
>>>>~~Bonnie
>>>>
>>>>
>>>>
>>>>>Dear Bonnie,
>>>>>
>>>>>Close, I guess.... but no cigar.
>>>>>
>>>>>Let me try to clarify:
>>>>>
>>>>>This is how I call the routine and define "cols"
>>>>>
>>>>>
>>>>>    Private Sub frmXXX_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
>>>>>        Me.MasKit(Me.XXXDataset.XXX.Columns)
>>>>>    End Sub
>>>>>
>>>>>
>>>>>so, cols is really Me.XXXDataset.XXX.Columns is a DataColumnCollection, belonging to a datatable ... I think.
>>>>>
>>>>>in my baseform I have :
>>>>>
>>>>>    Friend Sub MasKit(ByVal cols As DataColumnCollection)
>>>>>        Dim ctrl2 As IControlMaskit = Nothing
>>>>>        For Each Ctrl As Control In Me.Controls
>>>>>            Dim lApplicable As Boolean = True
>>>>>            Try
>>>>>                ctrl2 = Ctrl
>>>>>            Catch 
>>>>>                lApplicable = False
>>>>>            End Try
>>>>>            If lApplicable Then
>>>>>                ctrl2.MaskIt(cols)
>>>>>            End If
>>>>>        Next
>>>>>    End Sub
>>>>>
>>>>>
>>>>>and at the txtBase level (inferring from your wise advice ...)
>>>>>
>>>>>
>>>>>    Friend Sub MaskIt(ByVal cols As DataColumnCollection) Implements IControlMaskit.MaskIt
>>>>>        If Me.Mask <> "" Then
>>>>>            Exit Sub
>>>>>        End If
>>>>>
>>>>>        Dim B As Binding = Me.DataBindings.Item(0)
>>>>>        Dim t As DataTable = CType(B.DataSource, DataTable)
>>>>>        Dim c As DataColumn = Nothing
>>>>>
>>>>>        If t Is Nothing Then
>>>>>            Dim t1 As DataView = B.DataSource
>>>>>            c = t1.Columns(B.BindingMemberInfo.BindingField)
>>>>>        Else
>>>>>            c = t.Columns(Me.DataBindings.Item(0).BindingMemberInfo.BindingField)
>>>>>        End If
>>>>>
>>>>>        ' Dim o As DataColumn = cols(Me.DataBindings.Item(0).BindingMemberInfo.BindingField)
>>>>>        Dim cT As String = c.DataType.ToString
>>>>>        If cT = "System.String" Then
>>>>>            Me.Mask = ">" & New String("A", c.MaxLength)
>>>>>            Exit Sub
>>>>>        End If
>>>>>        If cT = "System.DateTime" Then
>>>>>            Me.Mask = ">##/AAA/##"
>>>>>            Exit Sub
>>>>>        End If
>>>>>        MsgBox(Me.Name & " cannot be masked yet (" & cT & ")")
>>>>>    End Sub
>>>>>
>>>>>
>>>>>Now this won't compile because "columns is not a member of 'system.data.dataview', and leaving out the dataview section does not work. (I don't think I need the dataview section). When I debug, B.Datasource does not seem to be set (in fact it returns "nothing", your "null" I guess?)
>>>>>
>>>>>I get the feeling from what I read in my frmXXX.designer.vb that the datasource of the "binding" is a dataset, whereas you seemed to assume that it should be a datatable or a dataview.
>>>>>
>>>>>That is precisely the problem I guess, because the form generates a xxxdataset that and binds the controls to a datatable that belongs the xxxDataset and is called XXX and referred to as xxxDataset.xxx. And it's the latter that I would like get a hold of from my base class textbox.
>>>>>
>>>>>Thanks for caring and shoot away ....
>>>>>
>>>>>Marc
>>>>>
>>>>>>Marc,
>>>>>>
>>>>>>I'm not 100% sure exactly what you're trying to get at, but I'll give it a shot. What I'm not clear about in your post is what "cols" is. Here's a snippet of code that I have used in my TextBox sub-class (sorry, it's C#, so I hope you can follow it):
>>>>>>
>>>>>>
>>>>>>// oBinding is simply what the TextBox is bound to
>>>>>>// Me.DataBindings.Item(0) in your example
>>>>>>
>>>>>>int nRow = this.BindingContext[this.oBinding.DataSource].Position;
>>>>>>string field = this.oBinding.BindingMemberInfo.BindingField;
>>>>>>
>>>>>>DataTable table = null;
>>>>>>if (this.oBinding.DataSource is DataView)
>>>>>>{
>>>>>>	nRow = CommonFunctions.IndexOfRow((DataView)this.oBinding.DataSource, nRow);
>>>>>>	table = ((DataView)this.oBinding.DataSource).Table;
>>>>>>}
>>>>>>else
>>>>>>	if (this.oBinding.DataSource is DataTable)
>>>>>>		table = (DataTable)this.oBinding.DataSource;
>>>>>>
>>>>>>
>>>>>>It appears that you're also talking about a sub-classed control, I hope I am correct in that assumption. Since your question appears to be about whatever this "cols" thing is, I'm not sure if the above helps or not.
>>>>>>
>>>>>>If not, perhaps you can rephrase your question or post some more code.
>>>>>>
>>>>>>~~Bonnie
>>>>>>
>>>>>>
>>>>>>>I have found that
>>>>>>>
>>>>>>>Me.DataBindings.Item(0).BindingMemberInfo.BindingField
>>>>>>>
>>>>>>>
>>>>>>>gives me the name of the field a control is bound to.
>>>>>>>
>>>>>>>
>>>>>>>Dim o As DataColumn = cols(Me.DataBindings.Item(0).BindingMemberInfo.BindingField)
>>>>>>>
>>>>>>>
>>>>>>>gets me the column, or the field that I am looking for.
>>>>>>>
>>>>>>>cols is passed to the method from the form's dataset.datatable (I guess).
>>>>>>>
>>>>>>>Is there a way for a textbox to let it's findform produce these (dataset.datatable).cols?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform