Find a row in dataGridView based on column and value

前端 未结 7 1405
轻奢々
轻奢々 2020-12-07 14:54

I have a dataGridView that has 3 columns: SystemId, FirstName, LastName that is bound using database information. I would like to highlight a certain row, which I would do

相关标签:
7条回答
  • 2020-12-07 15:17

    This builds on the above answer from Gordon--not all of it is my original work. What I did was add a more generic method to my static utility class.

    public static int MatchingRowIndex(DataGridView dgv, string columnName, string searchValue)
            {
            int rowIndex = -1;
            bool tempAllowUserToAddRows = dgv.AllowUserToAddRows;
    
            dgv.AllowUserToAddRows = false; // Turn off or .Value below will throw null exception
            if (dgv.Rows.Count > 0 && dgv.Columns.Count > 0 && dgv.Columns[columnName] != null)
                {
                DataGridViewRow row = dgv.Rows
                    .Cast<DataGridViewRow>()
                    .FirstOrDefault(r => r.Cells[columnName].Value.ToString().Equals(searchValue));
    
                rowIndex = row.Index;
                }
            dgv.AllowUserToAddRows = tempAllowUserToAddRows;
            return rowIndex;
            }
    

    Then in whatever form I want to use it, I call the method passing the DataGridView, column name and search value. For simplicity I am converting everything to strings for the search, though it would be easy enough to add overloads for specifying the data types.

    private void UndeleteSectionInGrid(string sectionLetter)
            {
            int sectionRowIndex = UtilityMethods.MatchingRowIndex(dgvSections, "SectionLetter", sectionLetter);
            dgvSections.Rows[sectionRowIndex].Cells["DeleteSection"].Value = false;
            }
    
    0 讨论(0)
  • 2020-12-07 15:18

    If you just want to check if that item exists:

    IEnumerable<DataGridViewRow> rows = grdPdfs.Rows
                .Cast<DataGridViewRow>()
                .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue));
    if (rows.Count() == 0) 
    {
        // Not Found
    } 
    else 
    {
        // Found
    }
    
    0 讨论(0)
  • 2020-12-07 15:20

    This will give you the gridview row index for the value:

    String searchValue = "somestring";
    int rowIndex = -1;
    foreach(DataGridViewRow row in DataGridView1.Rows)
    {
        if(row.Cells[1].Value.ToString().Equals(searchValue))
        {
            rowIndex = row.Index;
            break;
        }
    }
    

    Or a LINQ query

    int rowIndex = -1;
    
            DataGridViewRow row = dgv.Rows
                .Cast<DataGridViewRow>()
                .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue))
                .First();
    
            rowIndex = row.Index;
    

    then you can do:

    dataGridView1.Rows[rowIndex].Selected = true;
    
    0 讨论(0)
  • 2020-12-07 15:21

    Those who use WPF

    for (int i = 0; i < dataGridName.Items.Count; i++)
    {
          string cellValue= ((DataRowView)dataGridName.Items[i]).Row["columnName"].ToString();                
          if (cellValue.Equals("Search_string")) // check the search_string is present in the row of ColumnName
          {
             object item = dataGridName.Items[i];
             dataGridName.SelectedItem = item; // selecting the row of dataGridName
             dataGridName.ScrollIntoView(item);                    
             break;
          }
    }
    

    if you want to get the selected row items after this, the follwing code snippet is helpful

    DataRowView drv = dataGridName.SelectedItem as DataRowView;
    DataRow dr = drv.Row;
    string item1= Convert.ToString(dr.ItemArray[0]);// get the first column value from selected row 
    string item2= Convert.ToString(dr.ItemArray[1]);// get the second column value from selected row 
    
    0 讨论(0)
  • 2020-12-07 15:27

    Try this:

            string searchValue = textBox3.Text;
            int rowIndex = -1;
    
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            try
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells["peseneli"].Value.ToString().Equals(searchValue))
                    {
                        rowIndex = row.Index;
                        dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
                        dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    
                        break;
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
    
    0 讨论(0)
  • 2020-12-07 15:31

    Or you can use like this. This may be faster.

    int iFindNo = 14;
    int j = dataGridView1.Rows.Count-1;
    int iRowIndex = -1;
    for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count/2) +1; i++)
    {
        if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == iFindNo)
        {
            iRowIndex = i;
            break;
        }
        if (Convert.ToInt32(dataGridView1.Rows[j].Cells[0].Value) == iFindNo)
        {
            iRowIndex = j;
            break;
        }
        j--;
    }
    if (iRowIndex != -1)
        MessageBox.Show("Index is " + iRowIndex.ToString());
    else
        MessageBox.Show("Index not found." );
    
    0 讨论(0)
提交回复
热议问题