如何根据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;

            }

        }

转载于:https://www.cnblogs.com/pfcan66/archive/2012/09/17/2688835.html

你可能感兴趣的:(如何根据GridView中某列的值来选中该行,并将此行显示在GridView最顶端)