Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to jump down to first letter you type in Datagridview?
 

How to jump down to first letter you type in Datagridview?

In many list interfaces, you are able to type a letter and it jumps down to that row where the first letter of that instance occurs. An example is IE Favorites. How do you do this with DataGridView? Capture keys??
Jason Honingford  Thursday, June 21, 2007 8:20 PM

Hi Jason,

We can use the BindingSource.Find method to the row. Handle the DataGridView.KeyPress Method, and we must set the EditModel of DataGridView to DataGridViewEditMode.EditOnF2. See my sample below.

Code Snippet

DGV

public partial class DGVSearchItem : Form

{

public DGVSearchItem()

{

InitializeComponent();

}

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

private void DGVSearchItem_Load(object sender, EventArgs e)

{

dt.Columns.Add("fldID");

dt.Columns.Add("fldName");

for (int i = 0; i < 20; i++)

{

dt.Rows.Add(i.ToString(), "Name" + i.ToString());

}

bs.DataSource = dt.DefaultView;

this.dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;

this.dataGridView1.DataSource = bs;

this.dataGridView1.AllowUserToAddRows = false;

this.dataGridView1.KeyPress += new KeyPressEventHandler(dataGridView1_KeyPress);

}

void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)

{

DataView dv = new DataView(dt);

dv.RowFilter = "fldID like '" + e.KeyChar + "%'";

if (dv.Count > 0)

{

string x = dv[0][0].ToString();

this.dataGridView1.BindingContext[bs].Position = bs.Find("fldId", x);

}

}

}

Hi Jason,

We can use the BindingSource.Find method to the row. Handle the DataGridView.KeyPress Method, and we must set the EditModel of DataGridView to DataGridViewEditMode.EditOnF2. See my sample below.

Code Snippet

DGV

public partial class DGVSearchItem : Form

{

public DGVSearchItem()

{

InitializeComponent();

}

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

private void DGVSearchItem_Load(object sender, EventArgs e)

{

dt.Columns.Add("fldID");

dt.Columns.Add("fldName");

for (int i = 0; i < 20; i++)

{

dt.Rows.Add(i.ToString(), "Name" + i.ToString());

}

bs.DataSource = dt.DefaultView;

this.dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;

this.dataGridView1.DataSource = bs;

this.dataGridView1.AllowUserToAddRows = false;

this.dataGridView1.KeyPress += new KeyPressEventHandler(dataGridView1_KeyPress);

}

void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)

{

DataView dv = new DataView(dt);

dv.RowFilter = "fldID like '" + e.KeyChar + "%'";

if (dv.Count > 0)

{

string x = dv[0][0].ToString();

this.dataGridView1.BindingContext[bs].Position = bs.Find("fldId", x);

}

}

}

You can use google to search for other answers

Custom Search

More Threads

• Non-null constraint exception with unbound columns
• what is the proper way to update joined tables without concurrency violation
• Problem Capturing Tab Key Pressed
• Sort datatable
• Error "Impossible to analyse 'child' at position 1"
• Copy values from datagrid to database
• connect datagridview with datarow
• How to control border style of Datagridview?
• combo problem
• Maintaining the selected row after sorting a DataGridView