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....

Dani AI

Generated

A quick clarification first: the exception you hit (DataGridViewRow cannot be indexed) is because a DataGridViewRow does not have a default indexer. Access the value through the Cells collection, e.g. row.Cells("Status"), not row("Status"). Also, in WinForms you cannot truly disable a row; the supported pattern is to make it read-only and style it to look disabled.

Apply the rule right after binding so it persists through refreshes. The DataBindingComplete event is a good place, and you should skip the New Row placeholder:

Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
    For Each r As DataGridViewRow In DataGridView1.Rows
        If r.IsNewRow Then Continue For
        Dim status As String = Convert.ToString(r.Cells("Status").Value)
        Dim isReceived As Boolean = String.Equals(status, "Received", StringComparison.OrdinalIgnoreCase)
        r.ReadOnly = isReceived
        r.DefaultCellStyle.BackColor = If(isReceived, Color.LightGray, DataGridView1.DefaultCellStyle.BackColor)
    Next
End Sub

For a hard stop against editing regardless of style, also guard in CellBeginEdit:

Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
    Dim r = DataGridView1.Rows(e.RowIndex)
    If String.Equals(Convert.ToString(r.Cells("Status").Value), "Received", StringComparison.OrdinalIgnoreCase) Then
        e.Cancel = True
    End If
End Sub

Docs: DataGridViewRow.ReadOnly, DataGridView.DataBindingComplete, and DataGridView.CellBeginEdit.

Recommended Answers

All 9 Replies

datagridview1.rows=0

DataGridView1.Rows(Index).Visible = False

DataGridView1.Columns(Index).Visible = False

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

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

Can anyone help me in this????

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.