开发者

DataGridView binded to DataTable - Now sorting DataGridView

At the moment I have a DataGridView (bindet to a DataTable) and some labels. If DataGridView's event SelectionChanged occurs, the labels should be filled with the information of the selected row -> No problems. But if I sort the DataGridView and click on a row, the informations in the labels are wrong.

Event:

Private Sub DGVMain_SelectionChanged(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles DGVMain.SelectionChanged
    If DGVMain.SelectedRows.Count > 0 AndAlso Not Me.DGVMain Is Nothing Then
        Dim index As Integer = DGVMain.SelectedRows(0).Index

        Dim row As ResultSet.ResultsRow = CType(DGVMain.DataSource, ResultSet.ResultsDataTable).Rows(index)
        SetDetails(row)
    End If
End Sub

Sets the details of labels:

Private Sub SetDetails(ByVal row As ResultSet.ResultsRow)
    Dim rounding As Integer = 0
    If row.AssayDisplayResultFormat.Contains(":") Then
        rounding = Integer.Parse(row.AssayDisplayResultFormat.Split(":")(1))
    End If
    LBResultValue.Text = Round(row.ResultResultValue * row.AssayDisplayResultFactor, rounding) & " " & row.AssayDisplayResultUnit
    LBAssayNameValue.Text = row.AssayDisplayShortName & " (" & row.Assa开发者_StackOverflowyOID & ")"
    LBSampleIdValue.Text = row.ResourceName
    LBCreationDateValue.Text = row.ResourceCreated
    LBFlagsValue.Text = ""
    LBRawValue.Text = Round(row.ResultRawValue, rounding) & " " & row.AssayDisplayResultUnit
End Sub


you also need to set the details of label in the Sorted event of the DataGridView

        private void DGVMain_Sorted(object sender, EventArgs e)
        {
            SetDetails(dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex]);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜