The answer to my question was:
private void dg_CellClick(object sender, DataGridViewCellEventArgs e)
{
tblNamesBS.Position = e.RowIndex;
}
I'm a new programmer and new to stackoverflow!
I'm working in c# on a windows form application which pulls data from an SQL table into a datagridview. My form allows me to:
Search the SQL table with my form by typing a 'Customer Name' which then populates my datagridview with all search results.
Display the current row position search results into textboxes through BindingSource.
cycle through the search results by pressing my 'Next' or 'Previous' buttons using .MoveNext() and .MovePrevious()
What I haven't been able to do is change the current row position by simply mouse clicking. Instead, I HAVE TO click the 'Next' or 'Previous' buttons to cycle between the results.
So my question is, how can I change the current row position by mouseclicking?
Here is a little snippet of my code if it helps. I've got 16 databindings but I've only included one here to keep the code length small.
string Result = ComboSearch.Text;
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(sqlconnect);
SqlDataAdapter da = new SqlDataAdapter();
con.Open();
da.SelectCommand = new SqlCommand("SELECT * FROM TestDataTable WHERE
CustomerName LIKE + '%' + '" + Result + "' + '%' ", con);
ds.Clear();
da.Fill(ds);
dg.DataSource = ds.Tables[0];
TxtBoxCustomer.DataBindings.Clear();
BindingSource tblNamesBS = new BindingSource();
TxtBoxCustomer.DataBindings.Add(new Binding("Text", tblNamesBS,
"CustomerName"));
private void button3_Click(object sender, EventArgs e)
{
tblNamesBS.MoveNext();
dgUpdate();
}
private void button4_Click(object sender, EventArgs e)
{
tblNamesBS.MovePrevious();
dgUpdate();
}
private void dgUpdate()
{
dg.ClearSelection();
dg.Rows[tblNamesBS.Position].Selected = true;
dg.FirstDisplayedScrollingRowIndex = dg.SelectedRows[0].Index;
}