Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataSource, DisplayMember for Label or Textbox?

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Hi Folks

This is probably a dumb question, but I have a few labels on a form that are currently unbound, but display data from a data table.

I'd like to be able to feed these controls a code value (1, 2, 3) and have it display a related text value ("One", "Two", "Three") in the text of the control. Sort of like you can do with a ComboBox that has DataSource and DisplayMember properties.

Now I know that a standard label or textbox doesn't have these attributes, but if I wanted to add them how could I do that? I'm already assuming to create a small table with the lookup values to assign it to.

Appreciate any help you can throw at me...

Craig

CraigHartz
 
Are you saying you are setting the data in these labels like this?
Code:
=FIRST(Fields!SomeFieldName.Value, "SomeDataSetName")

My first suggestion is that I prefer to use a table to neatly arrange labels. Is there anyway you could construct a query to populate a table?

My second suggestion would be to use a VB function if these values are static. If you create a function like so:
Code:
    Function GetNumberString(ByVal Number As Integer) As String
        Select Case Number
            Case 1
                Return "One"
            Case 2
                Return "Two"
            Case 3
                Return "Three"
            Case Else
                Return ""
        End Select
    End Function
then you could use an expression like this:
Code:
=Code.GetNumberString(FIRST(Fields!SomeFieldName.Value, "SomeDataSetName"))


A third suggestion is if these are static would be to just use a derived table in your source query to get a string value, and use that in your labels
Code:
SELECT a.*, ISNULL(b.Display, '') AS Display
FROM YourTable a
LEFT OUTER JOIN
  (SELECT 1 As ID, 'One' AS Display
   UNION ALL
   SELECT 2 As ID, 'Two' AS Display
   UNION ALL
   SELECT 3 As ID, 'Three' AS Display) b
ON a.NumberColumn = b.ID
Then in your label, you can have something like the following
Code:
=FIRST(Fields!Display.Value, "SomeDataSetName")

If your values are not static, then yes, create a permanent table and use it instead of the derived table I showed above.
 
Hi RiverGuy

Thanks - unfortunately the values are not static, or yeah I would have set it up pretty much like you suggested (perhaps not as well).

But the values can be different, which is why I was hoping to figure out something that would take the code value and look up the descriptive value, then place it in the label / textbox.

Hmm, I think I just gave myself an idea to build a function, I'm going to go and try it to see if it will work. Thanks!

Craig

CraigHartz
 
Oh man. Craig, when I posted the reply, I thought I was in the SSRS forum, so basically most of my reply makes no sense as far as answering your question is concerned.

Anyways, if you do write a query which returns both your value and your description, you can bind both of those from the same row to your label as below:
Code:
        Me.Label1.DataBindings.Add("Text", DataTableName, "DisplayColumn")
        Me.Label1.DataBindings.Add("Tag", DataTableName, "IDColumn")

If you want to get the ID from the label, you would then look at it's .Tag property.
 
Hey RiverGuy

lol- oddly enough I sort of understood what you were trying to say!

In the end what I ended up doing was to create a function with a select query into which I pass the table name, the Code field, the Desc field, and the Code Value, and return the Desc value. Just a simple oledbCommand object in the function runs with an ExecuteScalar method and Bob's your Uncle, it's done. It actually works better than what I had planned from a code re-use standpoint, and the statements are more readable. I'm happy with it!

Craig

CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top