Question Sorting Datagrid Column

mac-duff

Member
Joined
Dec 21, 2009
Messages
24
Programming Experience
Beginner
Hello everyone,

I have a datagridview which I fill with a database. Sorting and everything works fine.

Now I addded a column manually which uses the hostname value of a database field to check if this PC is online. Works.

Now I want to order by status online and nothing happends. When I sort a database column the online staus disappears.

How can I migrate my manually fields to become a full member?


This is for example the code how I add the online status
VB.NET:
Private Sub PingWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles PingWorker.DoWork
        On Error Resume Next
        If My.Computer.Network.IsAvailable = True Then

            For Each row As DataGridViewRow In HardwareDataGridView.Rows
                row.Cells(StatusOnline.Index).Value = Nothing
            Next


            For Each row As DataGridViewRow In HardwareDataGridView.Rows
                'MessageBox.Show(CStr(row.Cells(DataGridViewTextBoxColumn32.Index).Value))
                If CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value) > -1 Then
                    If My.Computer.Network.Ping(CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value), 1000) = True Then
                        'MsgBox("Online")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Green
                        row.Cells(StatusOnline.Index).Value = "Online"

                    Else
                        'MsgBox("Offline")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Red
                        row.Cells(StatusOnline.Index).Value = "Offline"
                    End If
                End If

            Next

        End If

    End Sub

Hope someone can help me with that

Thx
 
First up, you should get rid of that "On Error Resume Next". There's no reason to ever using that in VB.NET. If you expect errors then handle them explicitly. If you don't expect errors then don't just add code to ignore errors just in case.

As for the question, I would suggest that you actually add a new column to your DataTable. You can then just bind as normal and everything will work normally.
 
Thanks,

this is working fine, just it is losing the color...

row.Cells(StatusOnline.Index).Value = "Offline"


Do u know why?


sorry, meinte:
row.Cells(StatusOnline.Index).Style.ForeColor = Color.Red
 
Last edited:
I found this article Cell Styles in the Windows Forms DataGridView Control but I can not really find a solution how I can change the color for the current cell, thats my trys:

HardwareDataGridView.CurrentCell.Style.ForeColor = Color.Green
Me.DataGridViewTextBoxColumn34.DefaultCellStyle.ForeColor = Color.Red

Me.HardwareDataGridView.CurrentCell = Me.HardwareDataGridView(DataGridViewTextBoxColumn34.Index, row.Index)
HardwareDataGridView.CurrentCell.Style.ForeColor = Color.Red
 
Last edited:
I did it now this way:
VB.NET:
    Private Sub StatusOnline()
        For Each row As DataGridViewRow In HardwareDataGridView.Rows
            If row.Cells(DataGridViewTextBoxColumn34.Index).Value Is DBNull.Value = False Then

                If row.Cells(DataGridViewTextBoxColumn34.Index).Value = "Online" Then
                    Me.HardwareDataGridView(DataGridViewTextBoxColumn34.Index, row.Index).Style.ForeColor = Color.Green
                Else
                    Me.HardwareDataGridView(DataGridViewTextBoxColumn34.Index, row.Index).Style.ForeColor = Color.Red
                End If

            End If
        Next

    End Sub

    Private Sub HardwareDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles HardwareDataGridView.CellContentClick
        StatusOnline()
    End Sub

Dont know if its a nice way
 
Back
Top