Quantcast
Viewing all articles
Browse latest Browse all 27101

How to Sort DataGridView

Okay I've been searching a lot on this topic and I've found some conflicting information. First, is it possible or impossible to sort a datagridview by clicking on the header of the column in the datagridview on a form. I've seen two replies to this which say it's possible but they don't say how, and more replies which say its not possible.

Below is a project that I plagiarized....er, uh, that is, I wrote it all by myself. :o

This is a very simple call log entry program where users enter information about the calls they receive. It works fine but the users say they want to sort the data by date. Which I cannot understand in the first place, because the data is enter chronologically by just using it. I don't understand how the ID field is now starting in row 1 at 89 and counting up through there and then reverts to 1 way down in the list. I went to great pains to make sure this database was completely empty and the ID field would start at 1 when we began this. The ID fields are complete but they are out of order when you view the database in the datagridview or even we I open the file up in MS Access itself.

Regardless of that I want to have the data show up in the datagridview in chronological order so sorting either on the date field or the ID field should do it. Can someone help me with what is needed in the code below to achieve that result.

Code:

Imports System.Data.OleDb

Public Class Form1

    Dim Con As System.Data.OleDb.OleDbConnection
    Dim DBName As String = "C:\Databases\CallLog.mdb"
    Dim EDITMODE As Boolean = False
    Dim NEWMODE As Boolean = False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Con = New OleDbConnection
        Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBName & ";"
        RefreshLV()
        Con.Close()
        AddNewRecord()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        RefreshLV()
    End Sub

    Sub RefreshLV()
        Try

            Dim SQL As String
            SQL = "SELECT * FROM CallLog"

            Dim DS As DataSet
            DS = New DataSet

            Con.Open()

            Dim oData As OleDbDataAdapter
            oData = New OleDbDataAdapter(SQL, Con)
            oData.Fill(DS)

            lvData.Items.Clear()
            lvData.Columns.Clear()

            For i As Integer = 0 To DS.Tables(0).Columns.Count - 1
                lvData.Columns.Add(DS.Tables(0).Columns(i).Caption, 75, HorizontalAlignment.Left)
            Next

            For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
                Dim xItem As New ListViewItem(DS.Tables(0).Rows(i)("ID").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("CallDate").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("Caller").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("Reason").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("DwgNo").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("AnsweredBy").ToString)
                xItem.SubItems.Add(DS.Tables(0).Rows(i)("TransferredTo").ToString)
                lvData.Items.Add(xItem)
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
            lvData.Items.Clear()

        Finally
            Con.Close()
        End Try

    End Sub

    Sub ClearTextBoxes()
        lblID.Text = ""
        DateTimePicker1.Text = Now
        txtCaller.Text = ""
        txtReason.Text = ""
        txtDwgNo.Text = ""
        txtAnsweredBy.Text = ""
        txtTransferredTo.Text = ""
    End Sub

    Private Sub AddNewRecord()
        ClearTextBoxes()
        lblID.Text = "NEW RECORD"
        EDITMODE = False
        NEWMODE = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If lvData.SelectedIndices.Count > 0 Then

            Dim ItemNo As Integer = lvData.SelectedIndices(0)
            EDITMODE = True
            NEWMODE = False

            lblID.Text = lvData.Items(ItemNo).SubItems(0).Text
            DateTimePicker1.Text = lvData.Items(ItemNo).SubItems(1).Text
            txtCaller.Text = lvData.Items(ItemNo).SubItems(2).Text
            txtReason.Text = lvData.Items(ItemNo).SubItems(3).Text
            txtDwgNo.Text = lvData.Items(ItemNo).SubItems(4).Text
            txtAnsweredBy.Text = lvData.Items(ItemNo).SubItems(5).Text
            txtTransferredTo.Text = lvData.Items(ItemNo).SubItems(6).Text
        Else
            ClearTextBoxes()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If EDITMODE = True Then

            Dim ID As String = Trim(lblID.Text)

            Try
                Con.Open()
                Dim cmd2 As New OleDb.OleDbCommand("UPDATE CallLog SET CallDate=@a0, Caller=@a1, Reason=@a2, DwgNo=@a3, AnsweredBy=@a4, TransferredTo=@a5 WHERE (ID=@a6) ", Con)
                cmd2.Parameters.AddWithValue("@a0", DateTimePicker1.Text)
                cmd2.Parameters.AddWithValue("@a1", txtCaller.Text)
                cmd2.Parameters.AddWithValue("@a2", txtReason.Text)
                cmd2.Parameters.AddWithValue("@a3", txtDwgNo.Text)
                cmd2.Parameters.AddWithValue("@a4", txtAnsweredBy.Text)
                cmd2.Parameters.AddWithValue("@a5", txtTransferredTo.Text)
                cmd2.Parameters.AddWithValue("@a6", ID)
                cmd2.ExecuteNonQuery()

                MsgBox("Record Updated Successfully")
                EDITMODE = False
                ClearTextBoxes()
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                Con.Close()
            End Try
        ElseIf NEWMODE = True Then

            Dim oAdapter As OleDb.OleDbDataAdapter
            Dim cb As OleDb.OleDbCommandBuilder
            Dim dr As DataRow
            Dim ds As DataSet
            Dim strSQL As String = "SELECT * FROM CallLog"

            ds = New DataSet()
            oAdapter = New OleDb.OleDbDataAdapter(strSQL, Con)
            oAdapter.Fill(ds)

            Try
                dr = ds.Tables(0).NewRow()
                dr.BeginEdit()

                dr("CallDate") = DateTimePicker1.Text
                dr("Caller") = txtCaller.Text
                dr("Reason") = txtReason.Text
                dr("DwgNo") = txtDwgNo.Text
                dr("AnsweredBy") = txtAnsweredBy.Text
                dr("TransferredTo") = txtTransferredTo.Text

                dr.EndEdit()

                ds.Tables(0).Rows.Add(dr)
                cb = New OleDb.OleDbCommandBuilder(oAdapter)
                oAdapter.InsertCommand = cb.GetInsertCommand
                oAdapter.Update(ds)
                ds.AcceptChanges()

                MessageBox.Show("Insert Successful")
                NEWMODE = False
                ClearTextBoxes()
            Catch ex As Exception
                MessageBox.Show(ex.Message)

            Finally
                Con.Close()
            End Try

        Else

        End If

        RefreshLV()
        AddNewRecord()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ClearTextBoxes()
        EDITMODE = False
        NEWMODE = False
        Con.Close()
        Me.Close()

    End Sub

End Class


Viewing all articles
Browse latest Browse all 27101

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>