简体   繁体   中英

C#, row count in datagridview on button click

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();
        }
    }

try块的底部try

TextBoxField.Text = dt.Rows.Count.ToString();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM