Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I would like to disable few rows in my gridview based on color. I have my 5th and 6th rows in Color.BlanchedAlmond. I want all the other rows to be disabled and I should not be able to click on it. How could i do that. Please do help me asap.

VB
For j = 0 To DataGridView1.RowCount - 1
    If DataGridView1.Rows(j).HasDefaultCellStyle = False Then
        DataGridView1.Rows(j).ReadOnly = True
    End If
Next
Posted
Updated 18-Jun-13 12:19pm
v2

1 solution

VB
Dim theColor As System.Drawing.Color = New System.Drawing.Color
theColor = System.Drawing.Color.BlanchedAlmound

Dim rowNum As Integer = 0

For rowNum=0 To DataGridView1.RowCount - 1
'check for new row if you are allowed to add a new row before the next lines if needed
If DataGridView1.Rows(rowNum).DefaultCellStyle.Backolor = theColor then
'do something
Else
DataGridView1.Rows(rowNum).ReadOnly = True
End If
Next


To clear any selection that is clicked, use the property and use .clearselection() to handle the selection event if it is needed.
 
Share this answer
 
v3
Comments
rahulaaditya 19-Jun-13 2:53am    
Dear Frn,

I tried your snippet. But still I was able to click all the rows and it does not help me out. For your information, I designed girdview with two columns. First column will contains files 1.zip and 2.zip. Second column will contain text "Download" with column type as DataGridViewLinkColumn.

So per condition I should be able to click on the Download link in my second row. And the first row, I should not be able to click on Download link. Hope this is clear now, in case if you have any queries please let me know.
PrissySC 20-Jun-13 13:46pm    
Have you changed what you need?

You said: I would like to disable few rows in my gridview based on color. I have my 5th and 6th rows in Color.BlanchedAlmond.

The above code disables any row that it is not of the background color. You need to add the handles for selection and clear any selection for any other row not of that background color.

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