简体   繁体   English

从一个单词搜索和无法访问的代码返回整个字符串

[英]Return the whole string from a one word search and unreachable code

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. 如果有人输入一个或两个单词,并且只输入字符串中的某些字符,则需要“ Column返回的整个字符串。

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 . 搜索是在第一个DataGridView Column中的TextBox完成的。它是一个存储在XML文件中的集合列表,该XML文件已加载到DataGridView

Also I have an i++ in for (int i = 0; i < row.Cells.Count; i++) that states it's unreachable. 另外我有一个i++for (int i = 0; i < row.Cells.Count; i++)指出它是不可到达的。
I am not sure why either. 我也不知道为什么。

It is just a binding list as a collection. 它只是一个绑定列表作为集合。

Picture of WinForms App WinForms应用程序的图片

//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++上的break并修复了隐藏代码,并删除了以row.Cells[0].Value替换的searchValue
I thought it was the searchValue and break issue. 我认为这是searchValue和break的问题。

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   
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM