Datagridview の列で 1 つのチェックボックスのみを強制的にチェックするにはどうすればよいですか?
15257 次
10 に答える
5
private void grdRegClass_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (grdRegClass.Columns.IndexOf(grdRegClass.Columns["Status"]) == e.ColumnIndex)
{
int currentcolumnclicked = e.ColumnIndex;
int currentrowclicked = e.RowIndex;
foreach (DataGridViewRow dr in grdRegClass.Rows)
{
dr.Cells[currentcolumnclicked].Value = false;
}
grdRegClass.CurrentRow.Cells[currentrowclicked].Value = true;
}
}
于 2014-08-09T14:40:26.750 に答える
3
グリッドのイベントをサブスクライブしCellValueChanged
、現在のセルのチェック状態に応じて、DataGridView をループし、他のセルの値として true/false を設定する必要があります。
void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
{
// Maybe have a method which does the
//loop and set value except for the current cell
}
}
}
于 2012-07-13T08:10:04.083 に答える
2
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);
}
}
于 2013-08-08T23:59:38.650 に答える
0
vb.netで:
Private Sub DataGridViewJobsList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewJobsList.CellValueChanged
If TypeOf TryCast(sender, DataGridView).CurrentCell Is DataGridViewCheckBoxCell Then
Dim cell1 As DataGridViewCell
cell1 = TryCast(sender, DataGridView).CurrentCell
If cell1.Value = True Then
For Each row As DataGridViewRow In TryCast(sender, DataGridView).Rows
If row.Index <> cell1.RowIndex AndAlso row.Cells(e.ColumnIndex).Value = "1" Then
row.Cells(e.ColumnIndex).Value = False
End If
Next
End If
End If
End Sub
Private Sub DataGridViewJobsList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridViewJobsList.CurrentCellDirtyStateChanged
If Me.DataGridViewJobsList.IsCurrentCellDirty Then
DataGridViewJobsList.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
于 2016-12-22T19:10:07.703 に答える
0
セルが変更された後に発生するため、CellEndEdit
のイベントを使用できます。DGV
以下のコードを使用するには、以下のコード スニペットのコメントをお読みください。
private void dgrvUserProfileView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int CheckedCount = 0;
//Make sure you have set True Value/ false Value property of check box column to 1/0 or true/false resp.
//Lets say your column 5th(namely Department) is a checked box column
if (dgrvUserProfileView.Columns[e.ColumnIndex].Name == "Department")
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
{
CheckedCount = CheckedCount + 1;
}
}
if (CheckedCount == 1)
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
{
dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = true;
}
}
}
else
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = false;
}
}
}
}
お役に立てれば!
于 2012-07-13T08:10:22.777 に答える
0
列を気にせずに失敗しないように、次のことを試してください。
private void DgvIVA_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dg = (DataGridView)sender;
if (dg.Rows.Count == 0) return;
if (dg.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxCell))
{
int rowSelIndex = e.RowIndex;
foreach (DataGridViewRow row in dg.Rows)
{
if (row.Index != rowSelIndex)
{
row.Cells[e.ColumnIndex].Value = false;
}
}
}
}
于 2021-05-26T04:00:49.577 に答える
0
private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int currentcolumnclicked = e.ColumnIndex;
for (int i = 0; i <= dgvlist.Columns.Count - 1; i++)
{
if (dgvlist.Columns[i] is DataGridViewCheckBoxColumn)
{
if (Convert.ToString(dgvlist.CurrentRow.Cells[i].EditedFormattedValue) == "True" && i !=currentcolumnclicked)
{
dgvlist.CurrentRow.Cells[i].Value = false;
}
}
}
}
于 2012-12-19T07:21:19.360 に答える
-2
private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
try
{
string val = dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (val == "False")
val = "True";
else if (val == "True")
val = "False";
dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val;
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
string active = "";
if (i != e.RowIndex)
{
if (val == "False")
{
dataGridView3.Rows[i].Cells[1].Value = "True";
active = "Y";
}
else if (val == "True")
{
dataGridView3.Rows[i].Cells[1].Value = "False";
active = "N";
}
}
else
{
if (val == "False")
active = "N";
else
active = "Y";
}
}
}
catch (Exception ex)
{ }
}
}
于 2013-11-12T09:39:31.057 に答える