如何根据GridView中某列的值来选中该行,并将此行显示在GridView最顶端

        // 根据ID选中行,并将选中行显示在GridView最顶端

        protected void SelectRowByID(DataGridView dgv, string ID)

        {

            //根据GridView某字段的值获得行号

            DataRow[] rows = (dgv.DataSource as DataTable).Select("ID='" + ID + "'");

            if (rows.Length > 0)

            {

                int findRow = (dgv.DataSource as DataTable).Rows.IndexOf(rows[0]);

                dgv.ClearSelection();

                if (findRow != -1)

                {

                      dgv.FirstDisplayedScrollingRowIndex = findRow;//将此行滚动到GridView最项端

                }

                dgv.Rows[findRow].Selected = true;

            }

        }

posted @ 2012-09-17 15:40  志誠  阅读(1908)  评论(0编辑  收藏  举报