Bindingsource.find

I’m sorry to ask something so basic but how do I get bindingsource.find to work

Below is the dreadful code I have in place and it always complains that the column name CarMake does not exist when it happily displays it as the column heading suggesting it does exist.

The actual error text is

DataMember property 'CarMake' cannot be found on the DataSource.

If you can help me gain momentum I’ll be very grateful. It's obvious I don't understand something but I'm not sure what.

Mike

Public Sub FillGrid()

'set up the dataset

Dim SQLTxt As String

Dim ConnStr As String

Dim FindTxt As String

FindTxt = ""

If Me.DataGridView1.SelectedCells.Count > 0 Then

FindTxt = Me.DataGridView1.SelectedCells(0).Value

End If

ConnStr = "Data Source=DELLMW\SQLEXPRESS;Initial Catalog=SuperVI;Integrated Security=SSPI;"

SQLTxt = "SELECT CarMake FROM CarMakes ORDER BY CarMake"

Dim Dap As New SqlDataAdapter(SQLTxt, ConnStr)

Dim DTable As New DataTable

Me.BindingSource1.DataSource = DTable

Dap.Fill(DTable)

Me.DataGridView1.AutoGenerateColumns = True

Me.DataGridView1.Sort(Me.DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

Me.DataGridView1.DataSource = Me.BindingSource1

Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)

Me.BindingSource1.DataSource.ToString()

If Len(FindTxt) > 0 Then

Me.BindingSource1.Find(DTable.Columns(0).ColumnName, FindTxt)

End If

End Sub



Answer this question

Bindingsource.find

  • Emanuel Dejanu

    Thanks for the advice but having tried it with

    BindingSource1.Find = String.Format("columnName = {0}", FindTxt)

    it won't compile. Maybe I should have said I'm using VB 2005 Express

    Mike


  • Dillon

    Thanks for taking the time to work out what I was trying to do and for your suggestion.

    But (there had to be one!) it laways comes back and tells me 'DataMember property 'CarMake' cannot be found on the DataSource' when CarMake is most certainly a valid field, I used autogenerte columns etc.

    I suppose my problem is that I have expected the binding source to have defaulted to include the CarMake as a column. OTOH the datatable associated with the dataset knows it has a column CarMake.

    I clearly don't understand something. I'm assuming that the bindingsource is clever enough to get the default column names and it isn't or it doesn't seem to be. So how do I persuade it to use the column best know as CarMake.

    I feel a real dolt for not getting this but I feel MS have made what should be intuitive unduly complex.

    Thanks again

    Mike


  • KumarG

    sorry my mistake

    its the wayof finding rows in the table not binding source you can try the following

    Me.BindingSource1.Find("ColumnName", value) if value is your variable that contain teh text, or int that you want

    sorry when i look to your  code closer i didn't understand what do you want to acomplish by it , actualy find function return an integer that you can use to change position but i didn't see your code used this returned value even you get the dataset name but you didn't use it either, so to change your binding source position you can do something like that

    Me.BindingSource1.Position = Me.BindingSource1.Find("ColName", "123")

    hope this helps



  • JEROMEMAS

    hi,

    your binding source find is like sql statment where clause, its just string "columnName = value and column2Name like '%value%' " if you change the criteria of searching in your program you can use

    bindingsource.find = string.format("columnName = {0} and column2Name like '%{1}%' " ,textbox1.text, textbox2.text)

    hope this helps



  • CBColin

    OK egg on face time.

    It was the order in which I did the fill and setting the datasource of the binding source

    Originally it was

    Me.BindingSource1.DataSource = DTable

    Dap.Fill(DTable)

    when it should have been.

    Dap.Fill(DTable)

    Me.BindingSource1.DataSource = DTable

    Which is quite logical really.

    I still think this is making the simple complex.

    Mike


  • Bindingsource.find