Sauced Apples
Sauced Apples

Reputation: 1173

Use Textbox to search DataGrid

I have been playing with this issue for a couple of hours and can't seem to get it to work.

It seems to not be searching the rows?

private void searchbutton_Click(object sender, EventArgs e)
    {
        string searchValue = searchtextBox.Text;
        int rowIndex = 0;

        inkGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        try
        {
            bool valueResulet = true;
            foreach (DataGridViewRow row in inkGridView.Rows)
            {
                if (row.Cells[rowIndex].Value.ToString().Equals(searchValue))
                {
                    rowIndex = row.Index;
                    inkGridView.Rows[rowIndex].Selected = true;
                    rowIndex++;
                    valueResulet = false;
                }
            }
            if (valueResulet != false)
            {
                MessageBox.Show("Unable to find "+ searchtextBox.Text,"Not Found");
                return;
            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }

It just always throws the error.

Upvotes: 0

Views: 2547

Answers (3)

NLindbom
NLindbom

Reputation: 508

It seems to me that your if-statement is the problem. Namely row.Cells[rowIndex] returns the Cells in each row and therefore what you named rowIndex and used in the if-statement is actually column index. So you can change it to, say:

if (row.Cells[0].Value.ToString().Equals(searchValue))

to search the first column. To check all Cell columns you have to loop over them in each row.

You might also wanna look over the use of rowIndex over all, since you first assigned it to 0 and incerement it in your loop. But, then you assign it to

rowIndex = row.Index;

and proceed to increment it (but only if you found a match). I think you just got your row and column indexes mixed up/intertwined.

Edit: And i presume the thrown exception is due to there not beeing enough columns as you loop over Cells (0, 0), (1, 1), (2, 2) ... and so forth instead of looping over rows with and a specified column.

Upvotes: 0

BunkerMentality
BunkerMentality

Reputation: 1337

I would say that unless you've set DataGridView.AllowUserToAddRows to false you are getting a null reference exception once you try to access Value.ToString() on the last row. So either set that to false or if you want to allow adding of new rows just add a check

if (row.IsNewRow) continue;

Upvotes: 0

maam27
maam27

Reputation: 434

I made a search textbox for a dataGridView I used so perhaps this is of use to you, since the controls are quite similar. although I chose to not give a message when it can't find it. Rather then a message it turns the textbox red, also it tries to find the first occurence of the complete text. If it can't find a complete match it will try to find a match that contains the search value

private void searchart()
{
  int itemrow = -1;
  String searchValue = cueTextBox1.Text.ToUpper();

  if (searchValue != null && searchValue != "")
    {
    foreach (DataGridViewRow row in dataGridView1.Rows)
      {
      //search for identical art
      if (row.Cells[0].Value.ToString().Equals(searchValue))
        {
          itemrow = row.Index;
          break;//stop searching if it's found
        }
      //search for first art that contains search value
      else if (row.Cells[0].Value.ToString().Contains(searchValue) && itemrow == -1)
      {
        itemrow = row.Index;
      }
    }
    //if nothing found set color red
    if (itemrow == -1)
    {
      cueTextBox1.BackColor = Color.Red;
    }
    //if found set color white, select the row and go to the row
    else
    {
      cueTextBox1.BackColor = Color.White;
      dataGridView1.Rows[itemrow].Selected = true;
      dataGridView1.FirstDisplayedScrollingRowIndex = itemrow;
    }
  }
}

Upvotes: 1

Related Questions