Hello Friends,

One help is required....

I have a select query which is based on two criterias....

if the order is in process or if the order is received....

The datagridview will show all the records from the database,,,,

I want to disable the rows in datagridview which will be in received status...only the on order should be editable....

is it possible???

please do help me in this...

Thanks in advance....

next we have the explanation
 DataGridView1.Rows.Add(row)
 row = New String() {"2", "Product 2", "2000"}
 DataGridView1.Rows.Add(row)
row = New String() {"3", "Product 3", "3000"}
 DataGridView1.Rows.Add(row)
 row = New String() {"4", "Product 4", "4000"}
 DataGridView1.Rows.Add(row)
 DataGridView1.Rows(1).Visible = False

sure, loop through the rows, and check the columns you have a condition, and set ReadOnly to true:

 For Each row As DataGridViewRow In dgv.Rows
    If row("ConditionColumnName").Value.ToString() = "your condition" Then
        row.[ReadOnly] = True
        'you can even color the row differently
        'like:
        row.DefaultCellStyle.BackColor = Color.Red
    Else
        row.[ReadOnly] = False
            'normal color
        row.DefaultCellStyle.BackColor = Color.White
    End If
Next

Edited 4 Years Ago by Mitja Bonca

Mitja - when I copied ur code I get an error that 'Class 'System.Windows.Forms.DataGridViewRow' cannot be indexed because it has no default property.'

See what I have done in my code.....

  Try
            Dim myCommand As SqlCommand
            myCommand = New SqlCommand("SELECT  * FROM PurchaseOrder ", Connection)
            Dim dt As New DataTable
            dt.Load(myCommand.ExecuteReader)
            If dt.Columns.Count <= 0 Then
                MsgBox("No records found")
            Else
                With DataGridView1
                    .AutoGenerateColumns = True
                    .DataSource = dt
                End With
            End If
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row("ConditionColumnName").Value.ToString() = "your condition" Then
                    row.[ReadOnly] = True
                    'you can even color the row differently
                    'like:
                    row.DefaultCellStyle.BackColor = Color.Red
                Else
                    row.[ReadOnly] = False
                    'normal color
                    row.DefaultCellStyle.BackColor = Color.White
                End If
            Next
        Catch ex As Exception
            'Debug.Print("Exception: ")
            Throw ex
        End Try

I created a DataGridView control with 2 columns and added a few records, then set row 2 to read only and modified the background colour with the following.

Private Sub btnPopulate_Click(sender As System.Object, e As System.EventArgs) Handles btnPopulate.Click

    grdView.Rows.Add({"George", "Jones"})
    grdView.Rows.Add({"Fred", "Flintstone"})
    grdView.Rows.Add({"John", "Franks"})
    grdView.Rows.Add({"Arthur", "Green"})

    grdView.Rows(2).ReadOnly = True

    For Each cell As DataGridViewCell In grdView.Rows(2).Cells
        cell.Style.BackColor = Color.LightGray
    Next

End Sub

Actually I know how to set it as readonly....what I want is according to the string in the column of datagridview as order or received the row shud be enabled and disabled....

Hello Friends....I got it working....
Have a look at the code....

'Open Connection

'Retrive records from database
Dim myCommand1 As New SqlCommand
With myCommand1
     .CommandText = "SELECT * FROM TableName"
     .CommandType = CommandType.Text
     .Connection = Connection
End With

'Fill the datagridview with the records
Dim dt As New DataTable
dt.Load(myCommand1.ExecuteReader)
If dt.Columns.Count > 0 Then
    With dgvDetails
         .DataSource = dt
    End With
End If

'Set readonly values to particular cell and set background color
For RCnt As Integer = 0 To dgvDetails.Rows.Count - 1
    If dgvDetails.Rows(RCnt).Cells("Status").Value = "InProgress" Then
          dgvDetails.Rows(RCnt).ReadOnly = False
          dgvDetails.Rows(RCnt).DefaultCellStyle.BackColor = Color.YellowGreen
    ElseIf dgvDetails.Rows(RCnt).Cells("Status").Value = "Received" Then
          dgvDetails.Rows(RCnt).ReadOnly = True
          dgvDetails.Rows(RCnt).DefaultCellStyle.BackColor = Color.Pink
    End If
Next

'Close connection
This question has already been answered. Start a new discussion instead.