Reputation: 191
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();
}
}
Upvotes: 0
Views: 963
Reputation: 486
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.
Upvotes: 0
Reputation: 1616
Try this at the bottom of the try
block:
TextBoxField.Text = dt.Rows.Count.ToString();
Upvotes: 3