Repi3

Hi all,

I have a problem with the bindingsource.find methode.
When i do this
Me.GemeenteBindingSource.Find("Gemeente", txtGemeente.text)

It wil return the index of the value of the txtgemeente.text box, but what must i do when i want to find a partcial nam

An example :

If txtGemeente = "Veurne" , i found the record and everything is fine, but when i search after txtGemeente = "Veur" then he doesn't find anything.

How can i resolve this problem

Thanx in advanced


Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

BonnieB

I don't have any way to test this at the moment, but could you try to use a wildcard and see if that works

txtGemeente = "Veur%"






Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

That doesn't works, because he's searching then after Veur%

In SQL , u can use the LIKE instruction, but here u can't do that.

I thought u could use something like :

txtGemeente = "Veur" & '%'

But he see's that as comments

Anyone





Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

BonnieB

I just tried messing around with it and I don't think it's going to work.

What exactly are you trying to accomplish Maybe we can find another way to do it ...






Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

What i want is :

if u typ a part of a town name in txtgemeente, he must go to the first record that has that.

Maybey we can use the filter function, that he just shows the towns that has that part of a value.

For example :

This records are in the DB
Veurne
Buslkamp
Veurton

So if u put Veur in the txtgemeente, u just must see nr1 & nr3

Any idee





Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

BonnieB

It sounds more like you need to use an AutoComplete ComboBox. If you are using VS2005, then try this combo class:

public class MyComboBoxAutoComplete : System.Windows.Forms.ComboBox
{
#region Declarations

private bool InEditMode = true;

#endregion

#region Constructor

public MyComboBoxAutoComplete()
{
this.DropDownStyle = ComboBoxStyle.DropDown;
this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.AutoCompleteSource = AutoCompleteSource.ListItems;
}
#endregion

#region Events

protected override void OnBindingContextChanged(EventArgs e)
{
this.DropDownStyle = ComboBoxStyle.DropDown;
base.OnBindingContextChanged(e);
}
protected override void OnValidating(CancelEventArgs e)
{
int index = this.FindStringExact(this.Text);
if (index >= 0 || this.Text.Trim() == "")
base.OnValidating(e);
else
e.Cancel = true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
this.InEditMode = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
base.OnKeyDown(e);
}
protected override void OnTextChanged(EventArgs e)
{
if (this.InEditMode)
{
int index = this.FindString(this.Text);
if (index < 0 && this.Text.Trim() != "")
{
this.InEditMode = false;
this.Text = this.Text.Substring(0, this.Text.Length - 1);
this.InEditMode = true;
this.Select(this.Text.Length, this.Text.Length);
}
}

base.OnTextChanged(e);
}

#endregion
}






Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

hm, i'm using VB.NET , and i think this is some C# code.

This is a bit above my hat :( , i just wanna use something like the '%' solution, if it's possible :p

someone that can help us





Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

BonnieB

No, the way you're trying to what you want is not going to work.

I'm pretty sure you want to use a ComboBox like the one I posted. There are websites that will convert C# to VB and vice-versa. Here's one I like to use:

http://www.carlosag.net/Tools/CodeTranslator/

Here's how it translated my C# code to VB:

Public Class MyComboBoxAutoComplete
    
Inherits System.Windows.Forms.ComboBox
    
    
Private InEditMode As Boolean = true
    
    Public Sub New
()
        
MyBase.New
        Me
.DropDownStyle ComboBoxStyle.DropDown
        
Me.AutoCompleteMode AutoCompleteMode.SuggestAppend
        
Me.AutoCompleteSource AutoCompleteSource.ListItems
    
End Sub
    
    Protected Overrides Sub 
OnBindingContextChanged(ByVal As EventArgs)
        
Me.DropDownStyle ComboBoxStyle.DropDown
        
MyBase.OnBindingContextChanged(e)
    
End Sub
    
    Protected Overrides Sub 
OnValidating(ByVal As CancelEventArgs)
        
Dim index As Integer = Me.FindStringExact(Me.Text)
        
If ((index >0)  _
                    
OrElse (Me.Text.Trim = "")) Then
            MyBase
.OnValidating(e)
        
Else
            
e.Cancel = true
        End If
    End Sub
    
    Protected Overrides Sub 
OnKeyDown(ByVal As KeyEventArgs)
        
Me.InEditMode ((e.KeyCode <> Keys.Back)  _
                    
AndAlso (e.KeyCode <> Keys.Delete))
        
MyBase.OnKeyDown(e)
    
End Sub
    
    Protected Overrides Sub 
OnTextChanged(ByVal As EventArgs)
        
If Me.InEditMode Then
            Dim 
index As Integer = Me.FindString(Me.Text)
            
If ((index < 0)  _
                        
AndAlso (Me.Text.Trim <> "")) Then
                Me
.InEditMode = false
                Me
.Text = Me.Text.Substring(0, (Me.Text.Length - 1))
                
Me.InEditMode = true
                Me
.Select(Me.Text.Length, Me.Text.Length)
            
End If
        End If
        MyBase
.OnTextChanged(e)
    
End Sub
End Class

 

Try the above code and see if helps you at all.


 






Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

So i bind a combo box to my DB, and if they type a part of the text , it will auto go to such records.

oke i get it, but in VB6 was it possible, with some code.

I should found it weird, if they doesn't put such option in that bindingsource.

Maybe we are being wrong, i hope :)





Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Ken Tucker

DataTable.Select  will return an array of datarows which match a search.  I would use datatable.select for the wildcard search and bindingsource.find to get the position of the first match returned from the datatable.select.

 



Imports System.Data.SqlClient

Public Class Form1
    Dim bs As New BindingSource
    Dim dt As New DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strConn As String = "Server = .\SQLEXPRESS;Database = Northwind;Integrated Security = SSPI;"

        Dim conn As New SqlConnection(strConn)
        Dim da As New SqlDataAdapter("Select * from Products", conn)
        da.Fill(dt)
        bs.DataSource = dt.DefaultView
        DataGridView1.DataSource = bs
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim dr() As DataRow = dt.Select("ProductName Like '" & TextBox1.Text & "%'", "ProductName")
        If dr.GetUpperBound(0) >= 0 Then
            Dim p As Integer
            p = bs.Find("ProductName", dr(0).Item("ProductName").ToString)
            bs.Position = p
        End If
    End Sub

   
End Class


 

 






Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

Hi,

This is an example with a SQL connection, can u give me 1 with ad database connection, because i'm trying, but i can't get it working.

Dim strConn2 As string OleDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\Database.mdb"

But i understand what u want to do, and i like it, because i have a good feeling :)

Thanx in advanced




Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Ken Tucker

How you get the data into the data table really does not matter.


Imports System.Data.OleDb
Public Class Form1
Dim bs As New BindingSource
Dim dt As New DataTable


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Nwnd.mdb"
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source = Northwind.mdb;"
Dim conn As New OleDbConnection(strConn)
Dim da As New OleDbDataAdapter("Select * from Products", conn)
da.Fill(dt)
bs.DataSource = dt.DefaultView
DataGridView1.DataSource = bs
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim dr() As DataRow = dt.Select("ProductName Like '" & TextBox1.Text & "%'", "ProductName")
If dr.GetUpperBound(0) >= 0 Then
Dim p As Integer
p = bs.Find("ProductName", dr(0).Item("ProductName").ToString)
bs.Position = p
End If
End Sub
End Class







Re: Windows Forms Data Controls and Databinding Bindingsource.Find problem

Repi3

oke, this is what a wanted, so BIG thanx.

I'm gonna look tomorrow, how this code works, but i think i know it already, and i figured out that it was no matter how it gets into the data table.

Thank u so much