I want only the comboBoxes that are in front of the checked checkboxes to be active and the others to be passive. My codes are those. How can I integrate these codes?
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
List ChkedRow = new List();
if (dataGridView1.Rows.Count > 0)
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["pid"].Value) == true)
{
ChkedRow.Add(i);
}
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Geziye katilacak personel seçimi yapilmadi. Geziye gidecek personeli seçmek için en bastaki kutucuklari isaretleyiniz!");
return;
}
DataGridViewComboBoxColumn comboBoxColumn = dataGridView1.Columns["kbaskani"] as DataGridViewComboBoxColumn;
if (comboBoxColumn != null)
{
int yesCount = 0;
int rowIndexWithMultipleYes = -1;
//Evet sayisini hesaplama ve birden fazla eveti hafizaya alma
for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
{
DataGridViewRow row = dataGridView1.Rows[rowIndex];
string cellValue = row.Cells[comboBoxColumn.Index].Value?.ToString();
if (cellValue == "Evet")
{
yesCount++;
if (yesCount > 1)
{
rowIndexWithMultipleYes = rowIndex;
break;
}
}
}
//Evet sayimina dayali sonuç gösterme
if (yesCount == 0)
{
MessageBox.Show("Kafile baskani seçmediniz. Bir kisi kafile baskani olarak seçilmelidir.", "UYARI", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (yesCount > 1)
{
MessageBox.Show("Birden fazla kafile baskani seçtiniz. Yalnizca bir kisi kafile baskani olarak seçilmelidir.", "UYARI", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (rowIndexWithMultipleYes != -1)
{
//1'den fazla kisi seçimi durumunda yapilacak uyari
for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
{
if (rowIndex != rowIndexWithMultipleYes)
{
dataGridView1.Rows[rowIndex].Cells[comboBoxColumn.Index].Value = "Hayir";
}
}
}
}
}
Mehmet FatihPosted Jul 30, 2023, 10:05 AM
Thanks CR Bhargavi. I have solved the problem with the following codes.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (value != null && value.ToString() != string.Empty)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if (Convert.ToBoolean(row.Cells[pid.Name].Value) == false)
{
MessageBox.Show("Öncelikle geziye katilacak kisi seçilmelidir.");
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (value != null && value.ToString() != string.Empty)
{
dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
}
}
Cr BhargaviPosted Jul 30, 2023, 2:39 AM
To highlight your Message box and others as passive, you should do these following, Add a CheckBox column to your `dataGridView1` to represent the selection status for each row. Attach an event handler to the CheckBox column to capture the check/uncheck events. In the event handler, enable or disable the corresponding ComboBox based on the checkbox status.
Here's how you can modify your code to achieve this:
Assuming the name of the CheckBox column is "cbxSelect" and the name of the DataGridViewComboBoxColumn is "kbaskani":
1. Add a CheckBox column to your `dataGridView1`. This can be done either programmatically or through the designer.
2. Attach an event handler to the CheckBox column to capture the check/uncheck events. In this example, let's name the event handler `cbxSelect_CheckedChanged`.
3. In the `cbxSelect_CheckedChanged` event handler, enable or disable the corresponding ComboBox based on the checkbox status.
Here's the modified code:
private void cbxSelect_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
// Get the current row index where the CheckBox is located
int rowIndex = dataGridView1.CurrentCell.RowIndex;
// Enable or disable the ComboBox based on the CheckBox status
if (checkBox.Checked)
{
dataGridView1.Rows[rowIndex].Cells["kbaskani"].ReadOnly = false;
}
else
{
dataGridView1.Rows[rowIndex].Cells["kbaskani"].ReadOnly = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
// Your existing code for button1_Click remains unchanged
// ...
}
Make sure to update the event handler assignment in your code when you add the CheckBox column to your `dataGridView1`. You can do this in the form's constructor or the Load event handler:
private void YourForm_Load(object sender, EventArgs e)
{
// Attach the event handler to the CheckBox column
DataGridViewCheckBoxColumn checkBoxColumn = (DataGridViewCheckBoxColumn)dataGridView1.Columns["cbxSelect"];
checkBoxColumn.CellValueChanged += cbxSelect_CheckedChanged;
}
With these modifications, the ComboBoxes will be enabled only for the rows where the corresponding CheckBox is checked, and they will be disabled for unchecked rows.