Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is working. The messageBox is showing when I click any cell on the row. But I want the messageBox to show when I click on the second cell instead of full row.


What I have tried:

C#
<pre>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 katılacak kişi seçilmelidir.");
                    }
                }
            }
Posted
Updated 30-Jul-23 1:35am

1 solution

You get the Row and Column indexes in the DataGridViewCellEventArgs Class (System.Windows.Forms) | Microsoft Learn[^]. So those two values will tell you which cell was clicked.
 
Share this answer
 
Comments
Member 12505620 30-Jul-23 9:48am    
Thanks. The problem was solved with the following codes.
if (dataGridView1.CurrentCell.ColumnIndex.Equals(1) && e.RowIndex != -1)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (value != null && value.ToString() != string.Empty)
{
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == false)
{
MessageBox.Show("Geziye katılmayan kişiyi kafile başkanı olarak seçmeye çalılmaktasınız.Öncelikle geziye katılacak kişi seçiniz.");
dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
}
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900