简体   繁体   English

C#,单击按钮时datagridview中的行数

[英]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. 我有一个清单应用程序,可在文本框中输入零件号并单击搜索按钮后,在datagridview中显示信息。 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. 使用e.rowindex可以告诉您所选行的索引。 If you are using button then you will need to use a global variable to determine which row has been selected. 如果使用的是按钮,则需要使用全局变量来确定选择了哪一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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