Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How To Make A WPF Datagrid Act Like A Spreadsheet

$
0
0

Greetings,

I posted an earlier similar question regarding this issue without finding a solution, but have since found a way to make a WPF DataGrid act like a spreadsheet.  Below you will find the user interface and some of the code to make it work.  I’m posting this just to inform, and especially to let Tibor Karaszi and Kalman Koth know that I was able to accomplish the goal of my earlier post.

The ‘Safety’ and ‘Balance’ columns are both calculated by code, and any changes made to the ‘Order Point’ the ‘On Hand’ textblocks or the ‘Quanity’ column will recalculate the whole spreadsheet when the ‘Recalculate’ button is pressed.

Here’s the partial code to accomplish this:

Private Sub ChosePart(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

        Dim lbsender As ListBox
        Dim li As ListBoxItem

        lbsender = CType(sender, ListBox)
        li = CType(lbsender.SelectedItem, ListBoxItem)
        tb.Text = "   You selected:  " & li.Content.ToString & "."
        Dim strLi As String = li.Content

        ' Initiate varitables:
        Dim intOrdPnt As Integer = 0
        Dim intOnHnd As Integer = 0
        Dim intBalance As Integer = 0
        Dim intSafety As Integer = 0
        Dim strSafety As String = ""
        Dim inDxRow As Integer = 0

        Select Case CStr(strLi)
            Case "RLM42HD Monitor, STERIS, 42in Medical LED w/ Protective Glass, Black"
                txtLeadTime.Text = "70 Days"
                txtOrderPoint.Text = 5
                txtOnHand.Text = 17
                WorkTable = "RLM42HD"

                ' Calculate Safety & Balance columns
                intOrdPnt = CInt(txtOrderPoint.Text)
                intOnHnd = CInt(txtOnHand.Text)
                Dim calCol = From cC In db.RLM42HD
                           Select cC.Id, cC.Quanity, cC.Balance

                Me.CompositeDataGrid.ItemsSource = db.RLM42HD.ToList

                For Each cC In calCol
                    'cC.Balance = intOnHnd + cC.Quanity     'cC Balance & Quanity are read only
                    intBalance = intOnHnd + cC.Quanity
                    intOnHnd = intBalance   ' intOnHand becomes the last row's balance
                    CType(CompositeDataGrid.Items(inDxRow), RLM42HD).Balance = intBalance
                    inDxRow += 1
                Next
                Try
                    db.SaveChanges()
                    'MsgBox("Saved new Balances.")
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
                inDxRow = 0
                For Each cC In calCol
                    If cC.Balance > intOrdPnt - 1 Then
                        strSafety = ""
                    Else
                        strSafety = CStr(cC.Balance - intOrdPnt)
                    End If
                    CType(CompositeDataGrid.Items(inDxRow), RLM42HD).Safety = strSafety
                    inDxRow += 1
                Next

            Case "VTP000002 Controller, Stack - CPU, ATOM"
                txtLeadTime.Text = "150 Days"
                txtOrderPoint.Text = 10
                txtOnHand.Text = 29
                WorkTable = "VTP000002"

Private Sub btnReCal_Click(sender As Object, e As RoutedEventArgs) Handles btnReCal.Click
        'Call PrintText()
        Try
            db.SaveChanges()
            MsgBox("Saved Database.")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        ' Initiate varitables:
        Dim intOrdPnt As Integer = 0
        Dim intOnHnd As Integer = 0
        Dim intBalance As Integer = 0
        Dim strSafety As String = ""
        Dim inDxRow As Integer = 0

        Select Case WorkTable
            Case "RLM42HD"
                GoTo RLM42HD
            Case "VTP000002"
                GoTo VTP000002
            Case "VTP000020"
                GoTo VTP000020
            Case "VTP000373"
                GoTo VTP000373
            Case "VTP000374"
                GoTo VTP000374
            Case "VTP002520"
                GoTo VTP002520
            Case "VTP002562"
                GoTo VTP002562
        End Select

RLM42HD:
        '    Case "RLM42HD Monitor, STERIS, 42in Medical LED w/ Protective Glass, Black"
        txtLeadTime.Text = "70 Days"
        txtOrderPoint.Text = 5
        txtOnHand.Text = 17
        WorkTable = "RLM42HD"

        ' Calculate Safety & Balance columns
        intOrdPnt = CInt(txtOrderPoint.Text)
        intOnHnd = CInt(txtOnHand.Text)
        Dim calCol = From cC In db.RLM42HD
                   Select cC.Id, cC.Quanity, cC.Balance

        Me.CompositeDataGrid.ItemsSource = db.RLM42HD.ToList

        For Each cC In calCol
            'cC.Balance = intOnHnd + cC.Quanity     'cC Balance & Quanity are read only
            intBalance = intOnHnd + cC.Quanity
            intOnHnd = intBalance   ' intOnHand becomes the last row's balance
            CType(CompositeDataGrid.Items(inDxRow), RLM42HD).Balance = intBalance
            inDxRow += 1
        Next
        Try
            db.SaveChanges()
            'MsgBox("Saved new Balances.")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        inDxRow = 0
        For Each cC In calCol
            If cC.Balance > intOrdPnt - 1 Then
                strSafety = ""
            Else
                strSafety = CStr(cC.Balance - intOrdPnt)
            End If
            CType(CompositeDataGrid.Items(inDxRow), RLM42HD).Safety = strSafety
            inDxRow += 1
        Next
        Me.CompositeDataGrid.ItemsSource = db.RLM42HD.ToList
        GoTo LoopOut

VTP000002:
        '    Case "VTP000002 Controller, Stack - CPU, ATOM"
        txtLeadTime.Text = "150 Days"
        txtOrderPoint.Text = 10
        txtOnHand.Text = 29
        WorkTable = "VTP000002"
Have fun!


Robert Agans


Viewing all articles
Browse latest Browse all 23857

Trending Articles