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
|