DataGridViewCheckBoxColumn的問題

來源:互聯網
上載者:User

標籤:

環境:VS2013

系統:win7 32位

問題:在DataGridView中使用列樣式DataGridViewCheckBoxColumn的問題:

(1)介面上通過滑鼠點擊,使DataGridViewCheckBoxColumn列的多個CheckBox狀態發生改變後,當滑鼠仍選中DataGridViewCheckBoxColumn列的某個儲存格時,擷取其狀態時,發現選中的儲存格值擷取結果錯誤;

擷取方法:dr.Cells["CheckItem"].Value.ToString()

            foreach (DataGridViewRow dr in DgvSoftUp.Rows)            {                if (dr.Cells["CheckItem"].Value != null)                {                    if (Convert.ToBoolean(dr.Cells["CheckItem"].Value.ToString()))                    {                         ……                    }                }            }

解決方案(來源於http://www.cnblogs.com/gossip/archive/2008/12/02/1346047.html):

在DataGridView的CurrentCellDirtyStateChanged事件中增加:DgvSoftUp.CommitEdit(DataGridViewDataErrorContexts.Commit);

        private void DgvSoftUp_CurrentCellDirtyStateChanged(object sender, EventArgs e)        {            DgvSoftUp.CommitEdit(DataGridViewDataErrorContexts.Commit);        }

 

(2)通過代碼設定CheckBox狀態時,當儲存格選中狀態時,狀態設定無效。

設定代碼如下:

           foreach (DataGridViewRow dr in DgvSoftUp.Rows)            {                 dr.Cells["CheckItem"].Value = true;            }

 解決辦法,在設定之後,增加EndEdit(): 

     DgvSoftUp.EndEdit();

 

DataGridViewCheckBoxColumn的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.