Как сделать поиск по DataGridView?
Категория: .NET
2011-09-03 17:19:01
code: #csharp
- private void searchDownBtn_Click( object sender, EventArgs e )
- {
- if (!newFind)
- {
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.BackColor = oldBackColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.SelectionBackColor = oldBackColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.ForeColor = oldForeColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.SelectionForeColor = oldForeColor;
- }
- if (searchTextBox.Text != null && searchTextBox.Text != String.Empty)
- {
- bool finded = false;
- int n = lastFindRow;
- lastFindCell++;
- for (int i = lastFindRow; i < dataGridView.Rows.Count; i++)
- {
- if (dataGridView.Rows[i].Visible)
- {
- if (i != lastFindRow)
- lastFindCell = 0;
- finded = false;
- for (int k = lastFindCell; k < dataGridView.Rows[i].Cells.Count; k++)
- {
- if (dataGridView.Rows[i].Cells[k].Value != null && dataGridView.Rows[i].Cells[k].Visible)
- {
- if (dataGridView.Rows[i].Cells[k].Value.ToString().IndexOf( searchTextBox.Text, StringComparison.OrdinalIgnoreCase ) >= 0)
- {
- lastFindCell = k;
- oldBackColor = dataGridView.Rows[i].Cells[k].Style.BackColor;
- oldForeColor = dataGridView.Rows[i].Cells[k].Style.ForeColor;
- finded = true;
- break;
- }
- }
- }
- if (finded)
- {
- dataGridView.Rows[i].Cells[lastFindCell].Style.SelectionBackColor = Color.Lime;
- dataGridView.Rows[i].Cells[lastFindCell].Style.SelectionForeColor = Color.Black;
- dataGridView.CurrentCell = dataGridView.Rows[i].Cells[lastFindCell];
- newFind = false;
- lastFindRow = i;
- break;
- }
- n = i;
- }
- }
- if (!finded)
- {
- newFind = true;
- lastFindRow = 0;
- lastFindCell = 0;
- if (n + 1 >= dataGridView.Rows.Count)
- {
- if (MessageBox.Show( "Поиск достиг конца таблицы! Начать поиск заново сначала?", "Информация!", MessageBoxButtons.YesNo, MessageBoxIcon.Information ) == DialogResult.Yes)
- {
- searchDownBtn.PerformClick();
- }
- }
- else
- MessageBox.Show( "Искомый текст не найден!", "Информация!", MessageBoxButtons.OK, MessageBoxIcon.Information );
- }
- }
- }
- private void searchUpBtn_Click( object sender, EventArgs e )
- {
- if (!newFind)
- {
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.BackColor = oldBackColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.SelectionBackColor = oldBackColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.ForeColor = oldForeColor;
- dataGridView.Rows[lastFindRow].Cells[lastFindCell].Style.SelectionForeColor = oldForeColor;
- }
- if (searchTextBox.Text != null && searchTextBox.Text != String.Empty)
- {
- bool finded = false;
- int n = lastFindRow;
- lastFindCell--;
- for (int i = lastFindRow; i >= 0; i--)
- {
- if (dataGridView.Rows[i].Visible)
- {
- if (i != lastFindRow)
- lastFindCell = dataGridView.Rows[i].Cells.Count - 1;
- finded = false;
- for (int k = lastFindCell; k >= 0; k--)
- {
- if (dataGridView.Rows[i].Cells[k].Value != null && dataGridView.Rows[i].Cells[k].Visible)
- {
- if (dataGridView.Rows[i].Cells[k].Value.ToString().IndexOf( searchTextBox.Text, StringComparison.OrdinalIgnoreCase ) >= 0)
- {
- lastFindCell = k;
- oldBackColor = dataGridView.Rows[i].Cells[k].Style.BackColor;
- oldForeColor = dataGridView.Rows[i].Cells[k].Style.ForeColor;
- finded = true;
- break;
- }
- }
- }
- if (finded)
- {
- dataGridView.Rows[i].Cells[lastFindCell].Style.SelectionBackColor = Color.Lime;
- dataGridView.Rows[i].Cells[lastFindCell].Style.SelectionForeColor = Color.Black;
- dataGridView.CurrentCell = dataGridView.Rows[i].Cells[lastFindCell];
- newFind = false;
- lastFindRow = i;
- break;
- }
- n = i;
- }
- }
- if (!finded)
- {
- newFind = true;
- lastFindRow = dataGridView.Rows.Count - 1;
- if (dataGridView.Rows.Count > 0)
- lastFindCell = dataGridView.Rows[0].Cells.Count - 1;
- if (n <= 0)
- {
- if (MessageBox.Show( "Поиск достиг начала таблицы! Начать поиск заново с конца?", "Информация!", MessageBoxButtons.YesNo, MessageBoxIcon.Information ) == DialogResult.Yes)
- {
- searchUpBtn.PerformClick();
- }
- }
- else
- MessageBox.Show( "Искомый текст не найден!", "Информация!", MessageBoxButtons.OK, MessageBoxIcon.Information );
- }
- }
- }
Поделиться: