c#实现在DataGrideView中正反向查找匹配的结果

直接上代码:

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void FindDataForm_FindEvent(object sender, FindEventArgs args)
        {
            bool ifbreak = false;
            if (FindedRowIndex == 0 && FindedColumnIndex == 0)
            {
                if (FindedRowIndex != dbView.CurrentCell.RowIndex)
                {
                    FindedRowIndex = dbView.CurrentCell.RowIndex;
                }
                if (FindedColumnIndex != dbView.CurrentCell.ColumnIndex)
                {
                    FindedColumnIndex = dbView.CurrentCell.ColumnIndex;
                }
            }
            #region 判断当前值是不是匹配结果,是直接查找下一个而不取当前值

            var currentVal = dbView.CurrentCell.Value.ToString();
            var RegexStr = args.val;
            MatchCollection r = null;
            if (args.isFullCharacter)
            {
                RegexStr = "^" + args.val + "$";
            }
            if (!args.isCase)
            {
                r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
            }
            else
            {
                r = Regex.Matches(currentVal, RegexStr);
            }

            #endregion
            if (args.direction == 0)
            {
                if (r.Count > 0)
                {
                    if (dbView.CurrentCell.ColumnIndex == 0)
                    {
                        FindedRowIndex = dbView.CurrentCell.RowIndex - 1;
                        if (FindedRowIndex == -1)
                        {
                            FindedColumnIndex = -1;
                        }
                        else {
                            FindedColumnIndex = dbView.Columns.Count - 1;
                        }
                    }
                    else {
                        FindedColumnIndex = dbView.CurrentCell.ColumnIndex - 1;
                    }
                }
                if (FindedRowIndex == dbView.Rows.Count)
                {
                    FindedRowIndex--;
                }
                if (FindedColumnIndex == dbView.Columns.Count)
                {
                    FindedColumnIndex--;
                }
                while (FindedRowIndex > -1)
                {
                    while (FindedColumnIndex > -1)
                    {
                        currentVal = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex].Value.ToString();
                        RegexStr = args.val;
                        r = null;
                        if (args.isFullCharacter)
                        {
                            RegexStr = "^" + args.val + "$";
                        }
                        if (!args.isCase)
                        {
                            r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
                        }
                        else
                        {
                            r = Regex.Matches(currentVal, RegexStr);
                        }

                        if (r.Count > 0)
                        {
                            dbView.CurrentCell = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex];
                            ifbreak = true;
                        }

                        //当遍历到一行的第一列时将行索引减一,列索引回到最大值(逐行扫描)
                        if (FindedColumnIndex == 0)
                        {
                            FindedRowIndex--;
                            //第一行的第一列,列索引不归零
                            if (FindedRowIndex > -1)
                            {
                                FindedColumnIndex = dbView.Columns.Count;
                            }
                        }
                        FindedColumnIndex--;

                        if (ifbreak)
                        {
                            break;
                        }
                    }

                    if (ifbreak)
                    {
                        break;
                    }
                }
            }
            else
            {
                if (r.Count > 0)
                {
                    if (dbView.CurrentCell.ColumnIndex == dbView.Columns.Count - 1)
                    {
                        FindedRowIndex = dbView.CurrentCell.RowIndex + 1;
                        if (FindedRowIndex == dbView.Rows.Count)
                        {
                            FindedColumnIndex = dbView.CurrentCell.ColumnIndex + 1;
                        }
                        else {
                            FindedColumnIndex = 0;
                        }
                    }
                    else
                    {
                        FindedColumnIndex = dbView.CurrentCell.ColumnIndex + 1;
                    }
                }
                if (FindedRowIndex == -1) {
                    FindedRowIndex++;
                }
                if (FindedColumnIndex == -1)
                {
                    FindedColumnIndex++;
                }
                while (FindedRowIndex < dbView.Rows.Count)
                {
                    if (FindedRowIndex == dbView.Rows.Count && FindedColumnIndex == dbView.Columns.Count)
                    {
                        MessageBox.Show("没有更多记录可供查询。");
                        break;
                    }
                    while (FindedColumnIndex < dbView.Columns.Count)
                    {
                        currentVal = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex].Value.ToString();
                        RegexStr = args.val;
                        r = null;
                        if (args.isFullCharacter)
                        {
                            RegexStr = "^" + args.val + "$";
                        }
                        if (!args.isCase)
                        {
                            r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
                        }
                        else
                        {
                            r = Regex.Matches(currentVal, RegexStr);
                        }

                        if (r.Count > 0)
                        {
                            dbView.CurrentCell = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex];
                            ifbreak = true;
                        }
                        FindedColumnIndex++;
                        //当遍历到一行的最后一列时将行索引加一,列索引归零(逐行扫描)
                        if (FindedColumnIndex == dbView.Columns.Count)
                        {
                            FindedRowIndex++;
                            //最后一行的最后一列,列索引不归零
                            if (FindedRowIndex < dbView.Rows.Count)
                            {
                                FindedColumnIndex = 0;
                            }
                        }
                       

                        if (ifbreak)
                        {
                            break;
                        }
                    }
                    if (ifbreak)
                    {
                        break;
                    }
                }
            }
            if (!ifbreak)
            {
                MessageBox.Show("未能找到更多结果。");
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值