views:

3538

answers:

3

Update

I decided to iterate through the Data.DataTable and trimmed the values there.


Utilizing SirDemon's post, I have updated the code a little bit:

Sub test(ByVal path As String)
    Dim oData As GSDataObject = GetDataObj(path)
    EmptyComboBoxes()
    Dim oDT As New Data.DataTable
    Try
     Dim t = From r In oData.GetTable(String.Format("SELECT * FROM {0}gsobj\paths ORDER BY keyid", AddBS(path))) Select r

     If t.Count > 0 Then
      oDT = t.CopyToDataTable
            For Each dr As Data.DataRow In oDT.Rows
                dr.Item("key_code") = dr.Item("key_code").ToString.Trim
                dr.Item("descript") = dr.Item("descript").ToString.Trim
            Next
      dataPathComboBox.DataSource = oDT
      dataPathComboBox.DisplayMember = "descript"
      dataPathComboBox.ValueMember = "key_code"
      dataPathComboBox.SelectedIndex = 0
      dataPathComboBox.Enabled = True
     End If
    Catch ex As Exception

    End Try
End Sub

This works almost as I need it to, the data is originally from a foxpro table, so the strings it returns are <value> plus (<Field>.maxlength-<value>.length) of trailing whitespace characters. For example, a field with a 12 character length has a value of bob. When I query the database, I get "bob_________", where _ is a space.

I have tried a couple of different things to get rid of the whitespace such as:

dataPathComboBox.DisplayMember.Trim()
dataPathComboBox.DisplayMember = "descript".Trim.

But nothing has worked yet. Other than iterating through the Data.DataTable or creating a custom CopyToDataTable method, is there any way I can trim the values? Perhaps it can be done in-line with the LINQ query?


Here is the code I have so far, I have no problem querying the database and getting the information, but I cannot figure out how to display the proper text in the ComboBox list. I always get System.Data.DataRow :

Try
    Dim t = From r In oData.GetTable("SELECT * FROM ../gsobj/paths ORDER BY keyid") _
            Select r

    dataPathComboBox.DataSource = t.ToList
    dataPathComboBox.SelectedIndex = 0
    'dataPathComboBox.DisplayMember = t.ToList.First.Item("descript")
    dataPathComboBox.Enabled = True
Catch ex As Exception
    Stop
End Try

I know that on the DisplayMember line the .First.Item() part is wrong, I just wanted to show what row I am trying to designate as the DisplayMember.

Thanks in advanced!

A: 

DisplayMember is intended to indicate the name of the property holding the value to be displayed.

In your case, I'm not sure what the syntax will by since you seem to be using a DataSet, but that should be

... DisplayMember="Item['descript']"  ...

in Xaml, unless you need to switch that at runtime in which case you can do it in code with

dataPathComboBox.DisplayMember = "Item['descript']"

Again, not 100% sure on the syntax. If you are using a strongly typed DataSet it's even easier since you should have a "descript" property on your row, but given hat your error indicates "System.DataRow" and not a custom type, I guess you are not.

Denis Troller
A: 

Because I can't figure out the underlying type of the datasource you are using I suggest you to change commented string to

dataPathComboBox.DisplayMember = t.ElementType.GetProperties.GetValue(0).Name

and try to determine correct index (initially it is zero) in practice.

Alexander Prokofyev
+1  A: 

I'm pretty sure your code tries to set an entire DataRow to a property that is simply the name of the Field (in a strongly type class) or a Column (in a DataTable).

dataPathComboBox.DisplayMember = "descript"

Should work if the DataTable contains a retrieved column of that name.

Also, I'd suggest setting your SelectedIndex only AFTER you've done the DataBinding and you know you actually have items, otherwise SelectedIndex = 0 may throw an exception.

EDIT: Trimming the name of the bound column will trim just that, not the actual bound value string. You either have to go through all the items after they've been bound and do something like:

dataPathComboBox.Item[i].Text = dataPathComboBox.Item[i].Text.Trim()

For each one of the items. Not sure what ComboBox control you're using, so the item collection name might be something else.

Another solution is doing that for each item when it is bound if the ComboBox control exposes an onItemDataBound event of some kind.

There are plenty of other ways to do this, depending on what the control itself offers and what you choose to do.

SirDemon
Note that my answers tend towards C# syntax, I've not used VB in a long time.
SirDemon
Yeah, I can pretty much convert C# -> VB.NET in my head. No worries ^^.
Anders