0

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;

    }
David Andrew Thorpe
  • 838
  • 1
  • 9
  • 23
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Aug 29 '18 at 12:09
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.position?view=netframework-4.7.2 may be of interest. – mjwills Aug 29 '18 at 12:12
  • @mjwills I don't understand how this has anything to do with 'SQL injection'. I googled it and it came up as a malicious attack... – David Andrew Thorpe Aug 29 '18 at 12:56
  • @mjwills Ahhh yes I can see what you mean and thanks for the document link, I have bookmarked it. However, my question is not regarding sql injection, it's about how I can mouseclick on the datagridview to select an entry which would then populate textboxs. – David Andrew Thorpe Aug 29 '18 at 13:24
  • My second link may help with that. – mjwills Aug 29 '18 at 13:39
  • @mjwills I did have a read of that thanks. I figured out that the solution was to put this code inside a cellclick event: tblNamesBS.Position = e.RowIndex; – David Andrew Thorpe Aug 29 '18 at 14:03

0 Answers0