C# 搜索包含在多个选项卡页中的DataGridView

C# 搜索包含在多个选项卡页中的DataGridView,c#,winforms,datagridview,tabs,C#,Winforms,Datagridview,Tabs,我在七个不同的选项卡中有一个或两个DataGridView。我有一个搜索框,在当前选中的选项卡中搜索datagridview时效果很好,但是跨多个选项卡搜索被证明是一个问题。第一次使用搜索时,什么也没有发生。之后,当找到正在搜索的子字符串时,将显示正确的选项卡,但是包含该子字符串的行不会高亮显示。如果我在不更改选项卡页面的情况下再次搜索,将突出显示正确的行。。。提前感谢您的帮助!以下是我的方法: private void searchBox_KeyDown(object sender, Key

我在七个不同的选项卡中有一个或两个DataGridView。我有一个搜索框,在当前选中的选项卡中搜索datagridview时效果很好,但是跨多个选项卡搜索被证明是一个问题。第一次使用搜索时,什么也没有发生。之后,当找到正在搜索的子字符串时,将显示正确的选项卡,但是包含该子字符串的行不会高亮显示。如果我在不更改选项卡页面的情况下再次搜索,将突出显示正确的行。。。提前感谢您的帮助!以下是我的方法:

 private void searchBox_KeyDown(object sender, KeyEventArgs e)
    {
        // Declare DGV
        DataGridView DGV = null;
        bool isFound = false;
        int tabIndex = -1;

        // When a key is pressed and it's the enter key...
        if (e.KeyCode == Keys.Enter)
        {
            // Loop through all tab pages
            for (int i = 0; i < tabs.TabCount; i++)
            {
                // Loop through all controls in the selected tab
                foreach (Control c in tabs.TabPages[i].Controls)
                {
                    // if the control is a datagridview, cast it as a datagridview, 
                    // and set DGV equal to it
                    if (c is DataGridView)
                    {
                        DGV = (DataGridView)c;
                        string name = DGV.Name;

                        // Pass in DGV to Search() to search all cells within it for 
                        // the term in the searchbox
                        isFound = Search(DGV);
                    }

                    if (isFound)
                        tabIndex = i;
                }
            }

            if (tabIndex >= 0)
            {
                tabs.SelectTab(tabIndex);
                tabs.SelectedTab.Show();
                highLightSearchRows();
            }
        }
    }
private void searchBox\u KeyDown(对象发送者,KeyEventArgs e)
{
//申报DGV
DataGridView DGV=null;
bool isFound=false;
int tabIndex=-1;
//当按下一个键时,它是回车键。。。
如果(e.KeyCode==Keys.Enter)
{
//循环浏览所有选项卡页面
对于(int i=0;i=0)
{
tabs.选择tab(tabIndex);
tabs.SelectedTab.Show();
highLightSearchRows();
}
}
}
这是我的搜索方法:

private bool Search(DataGridView dataGrid)
    {
        int i, j;
        bool retval = false;

        // Makes sure searchBox contains text before searching
        if (searchBox.TextLength <= 0)
            return false;

        string searchBoxText = searchBox.Text, sub1 = searchBox.Text.Substring(1);

        // Doesn't accept an empty string as a valid search parameter
        if (searchBoxText == "")
            return false;

        string searchForText = searchBoxText;

        // Starts looping from the bottom of the grid up
        // This makes FirstDisplayedScrollingRowIndex show the uppermost match in the grid
        for (i = dataGrid.Rows.Count - 1; i >= 0; i--)
        {
            // If any cells contain the search string, they are highlighted
            if (dataGrid.Rows[i].Cells.OfType<DataGridViewCell>()
                .Any(cell => ((dynamic)cell.Value).Contains(searchForText)))
                retval = true;

            // Deselect all rows in all datagrids in order for other rows to                
            // be programmatically selected
            dataGrid.Rows[i].Selected = false;
        }

        return retval;
    }
私有布尔搜索(DataGridView dataGrid)
{
int i,j;
bool-retval=false;
//确保搜索框在搜索前包含文本

如果(searchBox.TextLength您试过调试发生了什么吗?您能发布
DataGridView
设置吗?我知道有多个设置,但我认为大多数都是相同的?@Ivan Stoev是的,找到了子字符串,并显示了字符串所在的选项卡页,但是除非选中了该行所在的选项卡页,否则该行不会高亮显示。@KDeck呃,我正在使用Visual Studio 2013,有没有一种方法可以一次将它们全部复制?我在网上找不到任何注释非常长的内容。只有我的2cts:Tabpages没有显示,不会刷新其内容。因此,请先选择页面,然后选择行!
private void highLightSearchRows()
    {
        DataGridView DGV = null;
        int rowIndex = -2;

        foreach (Control c in tabs.SelectedTab.Controls)
        {
            // if the control is a datagridview, cast it as a datagridview,
            // and set DGV equal to it
            if (c is DataGridView)
            {
                DGV = (DataGridView)c;
                string name = DGV.Name;

                foreach (DataGridViewRow row in DGV.Rows)
                {
                    // If any cells contain the search string, they are highlighted
                    if (row.Cells.OfType<DataGridViewCell>()
                        .Any(cell => ((dynamic)cell.Value).Contains(searchBox.Text)))
                    {
                        // Makes selected row the top row in the view
                        rowIndex = row.Index;
                        row.Selected = true;
                        DGV.FirstDisplayedScrollingRowIndex = rowIndex;
                    }

                    if (rowIndex >= 0)
                    {
                        // Select the matching row
                        row.Selected = true;
                        rowIndex = -2;
                    }
                }
            }
        }
    }