0

I have an inventory application that displays the information in datagridview after entering a part number in a textbox and clicking a search button. My question is, how do I count the rows that are displayed and put the count in a textbox? Below is my code to display the rows that relate to the part number entered.

  private void searchPartbtn_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(partSearch.Text))
        {
            try
            {
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "SELECT * FROM Inventory WHERE PartNumber='" + partSearch.Text + "'";
                command.CommandText = query;

                connection.Close();
                OleDbDataAdapter db = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                db.Fill(dt);
                dataGridFB.DataSource = dt;

            }

            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
                connection.Close();
            }
            searchHide();
            connection.Close();
        }
    }
1
  • 1
    Use could use dt.Rows.Count which gives you count. Commented May 27, 2016 at 2:26

2 Answers 2

3

Try this at the bottom of the try block:

TextBoxField.Text = dt.Rows.Count.ToString();
Sign up to request clarification or add additional context in comments.

Comments

0

Use e.rowindex which will tell you the index of the selected row. If you are using button then you will need to use a global variable to determine which row has been selected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.