簡體   English   中英

從一個單詞搜索和無法訪問的代碼返回整個字符串

[英]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++)指出它是不可到達的。
我也不知道為什么。

它只是一個綁定列表作為集合。

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

刪除了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM