WinForm中DataGridView的快速查找及將指定行顯示到第一行


        /// <summary>
        /// 快速在已綁定的列表查詢車輛
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFind_Click(object sender, EventArgs e)
        {
            // Linq模糊查詢  
            IEnumerable<DataGridViewRow> enumerableList = this.dgvCarList.Rows.Cast<DataGridViewRow>();
            List<DataGridViewRow> list = (from item in enumerableList
                                          where item.Cells[2].Value.ToString().IndexOf(this.txtCarCode.Text) >= 0
                                          select item).ToList();

            // 恢復之前行的背景顏色為默認的白色背景  
            this.dgvCarList.Rows[beforeMatchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.White;

            if (list.Count > 0)
            {
                // 查找匹配行高亮顯示  
                int matchedRowIndex = list[0].Index;
                this.dgvCarList.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                this.beforeMatchedRowIndex = matchedRowIndex;
                //dgvCarList.CurrentCell = dgvCarList.Rows[matchedRowIndex].Cells[2];
                dgvCarList.FirstDisplayedScrollingRowIndex = matchedRowIndex;
            }
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM