C# DataGridView 移動/指定 目前選擇的欄位 (DataGridView – Focus a specific cell)

C# DataGridView 移動/指定 目前選擇的欄位 (DataGridView – Focus a specific cell)

C# DataGridView 移動/指定 目前選擇的欄位 (DataGridView – Focus a specific cell)

資料來源:https://stackoverflow.com/questions/4918904/datagridview-focus-a-specific-cell

原文問題: 
How to set focus on any specified cell in DataGridView? 
I was expecting a simple way like Focus(rowindex,columnindex) but it is not that easy.

DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex];//該欄位不能是隱藏欄位

我自己的相關實作:

        //---
        //啟用授權模板頁籤~模板資料列表顯示
        public String m_StrdgvSub0202_01_ext01 = "";
        public void showAuthorizationTemplate(bool blnmoveFocus = false)
        {
            String SQL = "";
            String StrID, StrName, StrDoorNum, StrType, StrLevel, StrStartTime, StrEndTime;
            cleardgvSub0202_01();
            //SQL = String.Format("SELECT at.id AS id,at.name AS name,ct.type_name AS type,at.level AS level,at.available_date_start AS start_time,at.available_date_end AS end_time,atd.doornum AS doornum FROM authorization_template AS at,card_type AS ct,(SELECT authorization_template_detailed_id AS id ,COUNT(authorization_template_detailed_id) AS doornum FROM authorization_template_detailed GROUP BY authorization_template_detailed_id) AS atd WHERE (at.type=ct.id) AND (at.id=atd.id) {0};", m_StrdgvSub0202_01_ext01);
            SQL = String.Format("SELECT Data.id AS id,Data.name AS name,Data.type AS type,Data.level AS level,Data.start_time AS start_time,Data.end_time AS end_time,Data.doornum AS doornum FROM ( SELECT main.id AS id,main.name AS name,main.type AS type,main.level AS level,main.start_time AS start_time,main.end_time AS end_time,sub.doornum AS doornum FROM ( SELECT at.id AS id,at.name AS name,ct.type_name AS type,at.level AS level,at.available_date_start AS start_time,at.available_date_end AS end_time FROM authorization_template AS at,card_type AS ct WHERE (at.type=ct.id) ) AS main LEFT JOIN (SELECT authorization_template_detailed_id AS id ,COUNT(authorization_template_detailed_id) AS doornum FROM authorization_template_detailed GROUP BY authorization_template_detailed_id) AS sub ON (sub.id=main.id) ) AS Data {0};", m_StrdgvSub0202_01_ext01);
            MySqlDataReader Reader_Data = MySQL.GetDataReader(SQL);
            while (Reader_Data.Read())
            {
                StrID = Reader_Data["id"].ToString();
                StrName = Reader_Data["name"].ToString();
                StrDoorNum = Reader_Data["doornum"].ToString();
                StrType = Reader_Data["type"].ToString();
                StrLevel = Reader_Data["level"].ToString();
                StrStartTime = Reader_Data["start_time"].ToString();
                StrEndTime = Reader_Data["end_time"].ToString();
                dgvSub0202_01.Rows.Add(false,StrID, StrName, StrDoorNum, StrType, StrLevel, StrStartTime, StrEndTime);
            }
            Reader_Data.Close();
            labSub0202_01.Text = Language.m_StrlabSub0102_02 + dgvSub0202_01.Rows.Count;//車輛列表計數總量顯示支援多國語系

            if (blnmoveFocus == true)
            {
                if (dgvSub0202_01.Rows.Count > 0)
                {
                    dgvSub0202_01.CurrentCell = dgvSub0202_01.Rows[dgvSub0202_01.Rows.Count - 1].Cells[2];
                }
            }
        }
        //---啟用授權模板頁籤~模板資料列表顯示


One thought on “C# DataGridView 移動/指定 目前選擇的欄位 (DataGridView – Focus a specific cell)

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *