[英]Return the whole string from a one word search and unreachable code
我在使用此代码时遇到问题。 它仅返回字符串中的第一个单词。 如果有人输入一个或两个单词,并且只输入字符串中的某些字符,则需要“ Column
返回的整个字符串。
搜索是在第一个DataGridView
Column
中的TextBox
完成的。它是一个存储在XML
文件中的集合列表,该XML
文件已加载到DataGridView
。
另外我有一个i++
在for (int i = 0; i < row.Cells.Count; i++)
指出它是不可到达的。
我也不知道为什么。
它只是一个绑定列表作为集合。
//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);
}
}
删除了i++
上的break并修复了隐藏代码,并删除了以row.Cells[0].Value
替换的searchValue
。
我认为这是searchValue
和break的问题。
感谢大家澄清问题。
//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
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.