DataGridView 使用CheckBox選中行


在winform中使用checbox很多。上次那個項目里就用到了,寫了一個不太好用,后來翻閱了一下微軟提供的樣碼,我覺得有必要給大家分享一下。

        // This event handler manually raises the CellValueChanged event
        // by calling the CommitEdit method.
        public void DataGridView1_CurrentCellDirtyStateChanged(object sender,
            EventArgs e)
        {
            if (DataGridView1.IsCurrentCellDirty)
            {
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        // If a check box cell is clicked, this event handler disables  
        // or enables the button in the same row as the clicked cell.
        public void DataGridView1_CellValueChanged(object sender,
            DataGridViewCellEventArgs e)
        {
            if (DataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxs")
            {


                DataGridViewCheckBoxCell checkCell =
                    (DataGridViewCheckBoxCell)DataGridView1.
                    Rows[e.RowIndex].Cells["CheckBoxs"]; 
                if ((Boolean)checkCell.Value)
                { 
                     //處理選中
//do something
} DataGridView1.Invalidate(); } }

 


免責聲明!

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



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