Click here to Skip to main content
16,022,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day!
I have a datagridview, Inside the datagridview they have 2 types font text color that is color black and color red. Please click the link below to see the sample.

sample — Postimages[^]

Now I want is! If I double click the row where the font text color black means read only. But! if I double click the row where the font text is color red it's show a message.

What I have tried:

I have a code here but it's not work for me.

C#
private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if (row.DefaultCellStyle.ForeColor == Color.Black)
                {
                  //no codes
                }
                else if (row.DefaultCellStyle.ForeColor != Color.Black)
                {
                    MessageBox.Show("red");
                }
                break;
            }
}
Posted
Comments
PIEBALDconsult 22-Jun-24 15:50pm    
If you want control of how things look, do not use a DataGridView.
A DataGridView is fine for a quick-and-dirty proof-of-concept, but is not suitable for public release.

Why are you looking at the colors of the row instead of including a column in your data that just whether or not the record should be ReadOnly? Then you just check the value of that column in the row and you've got your answer.
 
Share this answer
 

You need to select only the row that has been double clicked. Something along these lines.


C#
private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
   var selectedRow = dataGridView2.Rows[e.RowIndex];
   string msg = selectedRow.DefaultCellStyle.ForeColor == Color.Black ? "Black" : "Red";
   MessageBox.Show(msg);
 }
 
Share this answer
 
v2

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