I am having problems with this code. It returns only the first word in the string. l need the whole string returned in the Column
if someone enters one or both words plus only some characters in the string.
The search is done in a TextBox
from the first DataGridView
Column
in. It's a collection list stored in an XML
file loaded into the DataGridView
.
Also I have an i++
in for (int i = 0; i < row.Cells.Count; i++)
that states it's unreachable.
I am not sure why either.
It is just a binding list as a collection.
//Search DataGridview Button
private void button3_Click(object sender, EventArgs e)
{
string searchValue = searchtextBox.Text.ToLower(); /
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
bool valueResult = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
//if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().ToLower().Equals(searchValue))
if (row.Cells[i].Value != null && row.Cells[i].Value.ToString().ToLower().Contains(searchValue))
{
int rowIndex = row.Index;
dataGridView1.Rows[rowIndex].Selected = true;
valueResult = true;
searchResults.Text += "=> " + searchValue + " " + Environment.NewLine.Trim();
}
break;
}
}
if (!valueResult)
{
MessageBox.Show("Unable to find " + searchtextBox.Text, "Not Found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
return;
}
}
catch (Exception exc)box
{
MessageBox.Show(exc.Message);
}
}
Removed break and fixed hidden code on i++
, and removed searchValue
replaced with row.Cells[0].Value
.
I thought it was the searchValue
and break issue.
Thanks for everyone clarifying the problem.
//Search DataGridview Button
private void button3_Click(object sender, EventArgs e)
{
string searchValue = searchtextBox.Text.ToLower(); //simple search Full row from text box with button
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try //try to run the following code
{
bool valueResult = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//i++
for (int i = 0; i < row.Cells.Count; i++)//for loop to enable iteration throught the gridview rows
{
//if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().ToLower().Equals(searchValue))
if (row.Cells[i].Value != null && row.Cells[i].Value.ToString().ToLower().Contains(searchValue))
{
int rowIndex = row.Index;
dataGridView1.Rows[rowIndex].Selected = true;
valueResult = true;
searchResults.Text += "=> " + row.Cells[i].Value + " " + Environment.NewLine.Trim();//outputs search results to multi line textbox separated by commas and trimmed white space of
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.