DataGridView - how can I make a checkbox act as a radio button?

前端 未结 4 1309
后悔当初
后悔当初 2021-01-21 00:18

I have a Windows Forms app that displays a list of objects in a DataGridView.

This control renders bool values as checkboxes.

There\'s a set of three checkboxe

4条回答
  •  孤城傲影
    2021-01-21 00:57

      private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
            {
                if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
                {
                    foreach (DataGridViewRow row in (sender as DataGridView).Rows)
                    {
                        if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                        {
                            row.Cells[e.ColumnIndex].Value = false;
                        }
                    }
                }
            }
        }
    
        private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewClient.IsCurrentCellDirty)
            {
                dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    

提交回复
热议问题