BindingSource.Find by partial string?

E

Edwin Smith

Hello:

I have the following code which works on a DataGridView binding source:

private void textBox1_TextChanged(object sender, EventArgs e)
{
this.pATIENTSBindingSource.Position =
this.pATIENTSBindingSource.Position("Name", textBox1.Text);
}

This works fine if I type the entire search string. However I want to search
with a partial string such that if I type a "v" it takes me to the first V
in the dataGridView table. If I type an "e" after the "v" it takes me to the
first name that begins with "ve" and so on.

Is there a way to do this?

Thanks

Edwin
 
E

Edwin Smith

OOPS! The code should actually read:

private void textBox1_TextChanged(object sender, EventArgs e)
{
this.pATIENTSBindingSource.Position =
this.pATIENTSBindingSource.Find("Name", textBox1.Text);
}
 
C

ClayB

Here is some code that assumes the BindingSource.DataSource is a
DataTable and filters a second DataView to find partial matches if the
text is not a full match.

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text.Length > 0)
{
int loc = bSrc.Find("Col1", this.textBox1.Text);
if (loc == -1)
{
//assume datasource is a DataTable
DataView dv = new DataView(bSrc.DataSource as
DataTable);
dv.RowFilter = string.Format("[Col1] LIKE '{0}*'",
this.textBox1.Text);
if (dv.Count > 0)
{
loc = bSrc.Find("Col1", dv[0]["Col1"]);
}
}
bSrc.Position = loc;
}
}

==================
Clay Burch
Syncfusion, Inc.
 
E

Edwin Smith

Thanks much. I finally got this to work. I did need to modify it slightly to
get it working however.

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text.Length > 0)
{
int loc = pATIENTSBindingSource.Find("Name",
this.textBox1.Text);
if (loc == -1)
{
DataView dv = new DataView(this.patients.PATIENTS);
dv.RowFilter = string.Format("Name LIKE '{0}*'",
this.textBox1.Text);
if (dv.Count > 0)
{
loc = pATIENTSBindingSource.Find("Name", dv[0]["Name"]);
}
}
pATIENTSBindingSource.Position = loc;
}
}

Thanks again

Edwin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top