簡體   English   中英

使用C#在radGridView中搜索值只是在radGridView的第一行中找到值

[英]Search for value in radGridView using C# just finds values in first row of radGridView

我正在嘗試在C#中的radGridView中搜索值。 我找到了許多解決方案,但是由於某種原因,它只在GridView的第一行中進行搜索。 您可以在這里看到我的代碼:

private void btnSearch_Click(object sender, EventArgs e)
{
    if (txtSearch.Text == "")
    {
        MessageBox.Show("Please fill in the textbox!");
        return;
    }

    else
    {
        gridViewContact.SelectionMode = GridViewSelectionMode.FullRowSelect;

        foreach (GridViewRowInfo row in gridViewContact.Rows)
        {
            if (row.Cells[0].Value.ToString().Contains(txtSearch.Text) || row.Cells[1].Value.ToString().Contains(txtSearch.Text) || row.Cells[2].Value.ToString().Contains(txtSearch.Text))
            {
                row.IsSelected = true;
                break;
            }

            else
            {
                MessageBox.Show("Nothing found!");
                return;
            }
        }
    }
}

但是,如果我搜索一個值,它將始終顯示消息框“未找到...”。 除了當我搜索第一行中的值時。 因此,它只在第一行中找到值,而在其他任何地方都找不到。

有什么建議么?

PS:我正在使用Telerik控件

編輯:在這里您可以看到gridView:

在此處輸入圖片說明

我認為您的MessageBox放錯了位置。 嘗試這個:

private void btnSearch_Click(object sender, EventArgs e)
{
    if (txtSearch.Text == "")
    {
        MessageBox.Show("Please fill in the textbox!");
        return;
    }

    else
    {
        gridViewContact.SelectionMode = GridViewSelectionMode.FullRowSelect;

        bool found = false;
        foreach (GridViewRowInfo row in gridViewContact.Rows)
        {
            if (row.Cells[0].Value.ToString().Contains(txtSearch.Text) || row.Cells[1].Value.ToString().Contains(txtSearch.Text) || row.Cells[2].Value.ToString().Contains(txtSearch.Text))
            {
                found = true;
                row.IsSelected = true;
                break;
            }
        }
        if (!found)
        {
            MessageBox.Show("Nothing found!");
        }
    }
}

如果您使用的是Telerik網格視圖,建議不要使用自定義搜索行,而是使用Telerik搜索行。 對我來說,它就像一種魅力。

this.radGridView1.AllowSearchRow = true;

這是顯示搜索工作原理的屏幕截圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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