Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataGridView - Click on Non ReadOnly column

Status
Not open for further replies.

Halliarse

IS-IT--Management
Jan 8, 2007
213
GB
HELP!!

I have a dynamically created datagridview which is added to my form controla and repeatedly refreshed. The gridview has 3 sets of 8 columns but only displays a set of columns if the row count in each set is greeater then 0.

Initially the datagrodview was disabled as it was only intedned as a display tool, however, the users are now required to acknowledge an alram that goes of as row level should a certain criteria be met. As the dataGridView is refreshed every 9 minutes, so too is the alarm!

I need to be able to allow the user to click on any cell in columns 1, 9 or 17 so that I can turn the alarm off for a period of time, but I'm unable to get those columns to be accessible to the user and then monitor for a click on that cell!

My code is as below so any help or advice would be greatly appreaciated!

Many thanks in advance

Steve

Code:
genGridView.RowCount = MaxRowCount

        If ComputerCount Mod genGridView.RowCount > 0 Then
            ColumnCount = (Int(ComputerCount / genGridView.RowCount) + 1) * 8
        Else
            ColumnCount = (ComputerCount / genGridView.RowCount) * 8
        End If

        ColumnSet = ColumnCount / 8
        genGridView.ColumnCount = ColumnCount

        genGridView.Size = New Size(GridWidth * ColumnCount, Int(genGridView.RowCount * ColumnWidth))
        Me.Size = New Size(FormWidth * ColumnCount, Int((genGridView.RowCount + 0.7) * ColumnWidth))
        genGridView.ColumnHeadersVisible = False
        genGridView.RowHeadersVisible = False
        'genGridView.Enabled = False ' Taken this out for click red face
        genGridView.ReadOnly = True

        For col As Integer = 0 To genGridView.ColumnCount - 1

            ' Set column widths

            Dim column As DataGridViewColumn = genGridView.Columns(col)

            If col = 0 Or col = 8 Or col = 16 Then
                column.Width = 200
            Else
                If col = 7 Or col = 15 Or col = 23 Then
                    column.Width = 60
                Else
                    column.Width = 35
                End If
            End If

        Next

        For rw As Integer = 0 To genGridView.RowCount - 1
            Dim row As DataGridViewRow = genGridView.Rows(rw)
            row.Height = RowHeight
        Next

        For row As Integer = 0 To genGridView.RowCount - 1

            genGridView.Rows(row).Cells(0).Value = ComputerName(row)
            genGridView.Rows(row).Cells(0).Style.BackColor = Color.DarkSlateGray
            genGridView.Rows(row).Cells(0).Style.ForeColor = Color.White
            genGridView.Rows(row).Cells(0).Style.Font = New Font("Microsoft Sans Serif", 13, FontStyle.Bold)

            If ComputerSensor(row) > MaxTemp Then
                genGridView.Rows(row).Cells(0).Style.ForeColor = Color.Red
            End If

            If String.IsNullOrEmpty((ComputerName(row + genGridView.RowCount))) = False Then
                genGridView.Rows(row).Cells(8).Value = ComputerName(row + genGridView.RowCount)
                genGridView.Rows(row).Cells(8).Style.BackColor = Color.DarkSlateGray
                genGridView.Rows(row).Cells(8).Style.ForeColor = Color.White
                genGridView.Rows(row).Cells(8).Style.Font = New Font("Microsoft Sans Serif", 13, FontStyle.Bold)

                If ComputerSensor(row + genGridView.RowCount) > MaxTemp Then
                    genGridView.Rows(row).Cells(8).Style.ForeColor = Color.Red
                End If
            Else
                genGridView.Rows(row).Cells(8).Style.BackColor = Color.DarkSlateGray
            End If

            If String.IsNullOrEmpty((ComputerName(row + genGridView.RowCount + genGridView.RowCount))) = False Then
                genGridView.Rows(row).Cells(16).Value = ComputerName(row + genGridView.RowCount + genGridView.RowCount)
                genGridView.Rows(row).Cells(16).Style.BackColor = Color.DarkSlateGray
                genGridView.Rows(row).Cells(16).Style.ForeColor = Color.White
                genGridView.Rows(row).Cells(16).Style.Font = New Font("Microsoft Sans Serif", 13, FontStyle.Bold)

                If ComputerSensor(row + genGridView.RowCount + genGridView.RowCount) > MaxTemp Then
                    genGridView.Rows(row).Cells(16).Style.ForeColor = Color.Red
                End If
            Else
                If ColumnSet = 3 Then
                    genGridView.Rows(row).Cells(16).Style.BackColor = Color.DarkSlateGray
                End If
            End If

            ' Computer Up

            genGridView.Rows(row).Cells(1).Dispose()
            genGridView.Rows(row).Cells(1).Value = ""
            genGridView.Rows(row).Cells(1) = New DataGridViewImageCell
            genGridView.Rows(row).Cells(1).Style.BackColor = Color.DarkSlateGray
            genGridView.Rows(row).Cells(1).Style.Alignment = DataGridViewContentAlignment.MiddleCenter

            If ComputerUp(row) >= 500 Then
                genGridView.Rows(row).Cells(1).Value = My.Resources.redface
                My.Computer.Audio.Play(My.Resources.aoogah, AudioPlayMode.Background)
            Else
                genGridView.Rows(row).Cells(1).Value = My.Resources.greenface
            End If

            If String.IsNullOrEmpty((ComputerName(row + genGridView.RowCount))) = False Then

                genGridView.Rows(row).Cells(9).Dispose()
                genGridView.Rows(row).Cells(9).Value = ""
                genGridView.Rows(row).Cells(9) = New DataGridViewImageCell
                genGridView.Rows(row).Cells(9).Style.BackColor = Color.DarkSlateGray
                genGridView.Rows(row).Cells(9).Style.Alignment = DataGridViewContentAlignment.MiddleCenter

                If ComputerUp(row + genGridView.RowCount) >= 500 Then
                    genGridView.Rows(row).Cells(9).Value = My.Resources.redface
                    My.Computer.Audio.Play(My.Resources.aoogah, AudioPlayMode.Background)
                Else
                    genGridView.Rows(row).Cells(9).Value = My.Resources.greenface
                End If
            Else
                genGridView.Rows(row).Cells(9).Style.BackColor = Color.DarkSlateGray
            End If

            If String.IsNullOrEmpty((ComputerName(row + genGridView.RowCount + genGridView.RowCount))) = False Then

                genGridView.Rows(row).Cells(17).Dispose()
                genGridView.Rows(row).Cells(17).Value = ""
                genGridView.Rows(row).Cells(17) = New DataGridViewImageCell
                genGridView.Rows(row).Cells(17).Style.BackColor = Color.DarkSlateGray
                genGridView.Rows(row).Cells(17).Style.Alignment = DataGridViewContentAlignment.MiddleCenter

                If ComputerUp(row + genGridView.RowCount) >= 500 Then
                    genGridView.Rows(row).Cells(17).Value = My.Resources.redface
                    My.Computer.Audio.Play(My.Resources.aoogah, AudioPlayMode.Background)
                Else
                    genGridView.Rows(row).Cells(17).Value = My.Resources.greenface
                End If
            Else
                If ColumnSet = 3 Then
                    genGridView.Rows(row).Cells(17).Style.BackColor = Color.DarkSlateGray
                End If
            End If

 Next

        ' Display the grid

        Me.Controls.Remove(genGridView)
        Me.Controls.Add(genGridView)
        genGridView.ClearSelection()
        genGridView.Refresh()
 
Instead of this: genGridView.Enabled = False, un-comment the line and explicitly set it to Enabled: genGridView.Enabled = True.

If that doesn't work, search your code to see if there's anywhere else the grid is set to Enabled = False.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi JBenson, thanks for the response.

Apologies for my lack of reply, other projects have taken precedence on this one!

I've checked my code and there is no place where genGridView.Enabled = False is set!

Basically, what I need to be able to do is have the entire genGridView diasbled / ReadOnly with the exceptions of columns 1, 9 and 17. The cells within these columns are defined as DataGridViewImageCell.

I then need to have a click event on each cell within those respective columns and I'm unable to do that as the genGridView has been created dynamically. I've tried everything I can find online and nothing seems to work so any advice would be brilliant.

Mant thanks

Steve
 
Well, you can't disable individual columns in a datagridview. What you can do is enable the entire grid, set it to ReadOnly, then in the CellClick event, just check the index of the cell clicked and only take action for the indices desired:

Private Sub dgv1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellClick
If e.ColumnIndex = 1 or e.ColumnIndex = 9 or E.ColumnIndex = 17 Then
'do your stuff​
EndIf
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top