Search for value in DataGridView in a column

前端 未结 6 1433
名媛妹妹
名媛妹妹 2020-12-01 17:12

I want the user to be able to search for a number in a column in the DataGridView (dgv). The dgv can hold many records. Each record has a Project Number. So I want the user

6条回答
  •  心在旅途
    2020-12-01 18:05

    //     This is the exact code for search facility in datagridview.
    private void buttonSearch_Click(object sender, EventArgs e)
    {
        string searchValue=textBoxSearch.Text;
        int rowIndex = 1;  //this one is depending on the position of cell or column
        //string first_row_data=dataGridView1.Rows[0].Cells[0].Value.ToString() ;
    
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        try
        {
            bool valueResulet = true;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[rowIndex].Value.ToString().Equals(searchValue))
                {
                    rowIndex = row.Index;
                    dataGridView1.Rows[rowIndex].Selected = true;
                    rowIndex++;
                    valueResulet = false;
                }
            }
            if (valueResulet != false)
            {
                MessageBox.Show("Record is not avalable for this Name"+textBoxSearch.Text,"Not Found");
                return;
            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }
    

提交回复
热议问题