Link to home
Start Free TrialLog in
Avatar of Jennifer Barman
Jennifer BarmanFlag for United States of America

asked on

Unbound column is not populating in datagrid. Program is not acknowledging unit number column WinForms visual studio 2008

I have written a winform application. I am ALMOST done with it. It has some bugs that I just can't seem to figure out. The program looks off of a stored procedure that then looks off a view and then looks off a table which is uploaded via a agent job in sql server. The table has duplicate entries. The view eliminates these entries and then the stored procedure creates parameters to use in the app based off a vurtial table that is the view.

after clicking on a button.. then there is unit, collect and center that will populate from this view. then you click on another button and it is to populate with the text "NO MATCH" for every column in the datagrid there "should" be a matching No match.

The issue that I am having is first an error: image1

The second issue I am having is that the program seems to reject the last two records.. it does not seem to recognize them in the program..  

 User generated image




Imports System.IO 'for FilesStream class
Imports System.Drawing.Printing
Imports System.Drawing.Graphics
Imports System.Windows.Forms.DataGrid
Imports System.Data.DataSet
Imports System.Data.DataTable
Imports System.EventArgs
Imports System.Data.SqlClient
Public Class CICBC_Parvo
    Private oStringFormat As StringFormat
    Private oStringFormatComboBox As StringFormat
    Private oButton As Button
    Private oCheckbox As CheckBox
    Private oComboBox As ComboBox
    Private nTotalWidth As Int16
    Private nRowPos As Int16
    Private NewPage As Boolean
    Private nPageNo As Int32
    Private Header As String = "CICBC_PARVO"
    Private sUserName As String = "NAT LAB"
    Public DefaultPageSettings As PageSettings = New PageSettings()
    Public StringPageNo As String
    Public StringNumberOfPages As String
    Public BooleanNumOfPages As Boolean
    Private Sub Parameters_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)

    End Sub
    Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    End Sub
    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Focus()
    End Sub
    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Show()
        Me.TextBox1.Focus()


        For i As Integer = 0 To Column1.DataGridView.Rows.Count - 4
            Column1.DataGridView.Item(3, i).Value = "NO MATCH"
        Next
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim Found As Boolean
        If TextBox1.Text.Length.Equals(16) Then
            For i As Integer = 0 To Column1.DataGridView.Rows.Count - 4
                If Column1.DataGridView.Item(0, i).Value = TextBox1.Text Then
                    Found = True
                    My.Computer.Audio.Play("\\Stellent2\fin_wp\Groups\02-Public_IT\Shared_IT\Parvo_noise\beep.wav")
                    Dim dbcon As New SqlConnection("Data Source = PRIVATE
                    dbcon.Open()
                    Dim dbcmd As New SqlCommand
                    dbcmd.Connection = dbcon
                    dbcmd.CommandText = "Insert into Match_Table(Unit) Values ('" & TextBox1.Text & "')"
                    'dbcmd.CommandText = "Insert into Match_Table(COLLECT) = COLLECTDataGridViewTextBoxColumn"
                    dbcmd.ExecuteNonQuery()
                    'dbcmd.CommandText = "Insert into Match_Table(Center) Values ('" & LifeTec_EliteDataSet1.CICBC_PARVO_PROCEDURE.CENTERColumn.ToString & " ')"
                    MATCH_NOMATCHDataGrid.DataGridView.Item(3, i).Value = "MATCH"
                    ORDERDataGrid.DataGridView.Item(4, i).Value = TimeString
                    Me.TextBox1.Clear()
                    Exit For
                End If
            Next
            If Not Found Then
                My.Computer.Audio.Play("\\Stellent2\fin_wp\Groups\02-Public_IT\Shared_IT\Parvo_noise\nonono.wav")
            End If
            Me.TextBox1.Clear()
        End If
        Me.TextBox1.Focus()

    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

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

    End Sub

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

    End Sub

    Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub
    Private Sub PrintDocument1_BeginPrint1(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint

        oStringFormat = New StringFormat
        oStringFormat.Alignment = StringAlignment.Near
        oStringFormat.LineAlignment = StringAlignment.Center
        oStringFormat.Trimming = StringTrimming.EllipsisCharacter

        oStringFormatComboBox = New StringFormat
        oStringFormatComboBox.LineAlignment = StringAlignment.Center
        oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
        oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter

        oButton = New Button
        oCheckbox = New CheckBox
        oComboBox = New ComboBox

        nTotalWidth = 0
        For Each oColumn As DataGridViewColumn In DataGridView1.Columns

            nTotalWidth += oColumn.Width

        Next
        nPageNo = 1
        NewPage = True
        nRowPos = 0

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Static oColumnLefts As New ArrayList
        Static oColumnWidths As New ArrayList
        Static oColumnTypes As New ArrayList
        Static nHeight As Int16

        Dim nWidth, i, nRowsPerPage As Int16
        Dim nTop As Int16 = e.MarginBounds.Top
        Dim nLeft As Int16 = e.MarginBounds.Left

        If nPageNo = 1 Then

            For Each oColumn As DataGridViewColumn In DataGridView1.Columns

                nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)

                nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 3

                oColumnLefts.Add(nLeft)
                oColumnWidths.Add(nWidth)
                oColumnTypes.Add(oColumn.GetType)
                nLeft += nWidth

            Next

        End If

        Do While nRowPos < DataGridView1.Rows.Count - 1

            Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)

            If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then

                DrawFooter(e, nRowsPerPage)

                NewPage = True
                nPageNo += 1
                e.HasMorePages = True
                Exit Sub

            Else

                If NewPage Then

                    ' Draw Header
                    e.Graphics.DrawString(Header, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height + 3)

                    ' Draw Columns
                    nTop = e.MarginBounds.Top
                    i = 0
                    For Each oColumn As DataGridViewColumn In DataGridView1.Columns

                        e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                        e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
                        i += 1

                    Next
                    NewPage = False

                End If

                nTop += nHeight
                i = 0
                For Each oCell As DataGridViewCell In oRow.Cells

                    If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then

                        e.Graphics.DrawString(oCell.Value, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)

                    ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then

                        oButton.Text = oCell.Value.ToString
                        oButton.Size = New Size(oColumnWidths(i), nHeight)
                        Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
                        oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                        e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

                    ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then

                        oCheckbox.Size = New Size(14, 14)
                        oCheckbox.Checked = CType(oCell.Value, Boolean)
                        Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
                        Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
                        oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                        oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 4, Int32), CType((oBitmap.Height - oCheckbox.Height) / 4, Int32), oCheckbox.Width, oCheckbox.Height))
                        e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

                    ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then

                        oComboBox.Size = New Size(oColumnWidths(i), nHeight)
                        Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
                        oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                        e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                        e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)

                    ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then

                        Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
                        Dim oImageSize As Size = CType(oCell.Value, Image).Size
                        e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 4), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 4), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))

                    End If

                    e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))

                    i += 1

                Next

            End If

            nRowPos += 1
            nRowsPerPage += 1

        Loop

        DrawFooter(e, nRowsPerPage)

        e.HasMorePages = False

    End Sub

    Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)

        Dim sPageNo As String = nPageNo.ToString
        ' Right Align - User Name
        e.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)

        ' Left Align - Date/Time
        e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)

        ' Center  - Page No. Info
        e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)

    End Sub

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint

    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim PageSetupDialog1 As New PageSetupDialog

        PageSetupDialog1.PageSettings = DefaultPageSettings

        PageSetupDialog1.ShowDialog()

        Dim PrintPreviewDialog1 As New PrintPreviewDialog()

        BooleanNumOfPages = False

        PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings

        PrintPreviewDialog1.Document = PrintDocument1

        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Tab Then
            TextBox1.Clear()
        End If
    End Sub

    Private Sub TextBox1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles TextBox1.Layout

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub

    Private Sub DataGridView1_CellContentClick_2(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub FillToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)

    End Sub

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

    End Sub

    Private Sub FillToolStrip_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles FillToolStrip.ItemClicked

    End Sub

    Private Sub FillToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillToolStripButton1.Click
        Try
            Me.CICBC_PARVO_PROCEDURETableAdapter.Fill(Me.LifeTec_EliteDataSet1.CICBC_PARVO_PROCEDURE, New System.Nullable(Of Date)(CType(Start_datetimeToolStripTextBox1.Text, Date)), New System.Nullable(Of Date)(CType(End_datetimeToolStripTextBox1.Text, Date)))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Start_datetimeToolStripTextBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start_datetimeToolStripTextBox1.Click

    End Sub
End Class

Open in new window

parvo002.jpg
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jennifer Barman

ASKER

That got rid of the issue with image1 THANKS!! now issue 2... my NO MATCH is not populating to the last two records.. and when I run my app and place these units in the scanner input below box.. then they say they don't exist..??? that is impossible.. I can see them right there...

What could be cause this???
what is the case for match?
I am confused by your question?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay.. but the last two records in the app are saying they are not there.... Match actually populated into an empty field.. Not really an issue.. that is a little programming issue that can be overlooked.. but the issue of 2 of the units saying NO and not changing the NO Match to match will not be acceptable by our lab..
Any ideas?
I just checked the table and it "IS" in the table.. doesnt seem like it is related to the table more like some type of cut off because it is the last two in the grid..
i believe it is
For i As Integer = 0 To Column1.DataGridView.Rows.Count - 4   -----> Did you change it with -1  (in your image2  3 "no match"  are missing which is correct - your code do that..)
            Column1.DataGridView.Item(3, i).Value = "NO MATCH"
        Next
there will be some criteria for "NO MATCH" and  "MATCH"  in which you are not reffering to !!
As stated in my beginning statement..

"The second issue I am having is that the program seems to reject the last two records.. it does not seem to recognize them in the program..  "

When I enter in the exact unit that is "in" the grid of the last two units... it handles it as if it is not there..  and performs this statements of code:

 If Not Found Then
                My.Computer.Audio.Play("\\Stellent2\fin_wp\Groups\02-Public_IT\Shared_IT\Parvo_noise\nonono.wav")
            End If
            Me.TextBox1.Clear()
        End If
        Me.TextBox1.Focus()

**** Would you have any suggestions as to why the logic would not hit the last 2 records in the grid.. It should.. but doesent.. and it is ALWAYS the last 2.. no matter what unit numbers are there.
sorry sorry i stacked in "NO MATCH"  let me check it ...
Okay.. got it..

Thank you for your assistance.. it led me to all the corrections that I needed to make..

My program is working wonderfully!!!

All points go to you!

        Me.Show()
        Me.TextBox1.Focus()


        For i As Integer = 0 To Me.Column1DataGridViewTextBoxColumn.DataGridView.Rows.Count - 2
            Me.Column1DataGridViewTextBoxColumn.DataGridView.Item(1, i).Value = "NO MATCH"
        Next
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim Found As Boolean
        If TextBox1.Text.Length.Equals(16) Then
            For i As Integer = 0 To Column1DataGridViewTextBoxColumn.DataGridView.Rows.Count - 2
                If Column1DataGridViewTextBoxColumn.DataGridView.Item(0, i).Value = TextBox1.Text Then
                    Found = True

Open in new window

Ok i understand now ....Tell me one more thing..
Why are you using even here RowsCount-4 your are limiting your data...in you datagridview ....despite that data are already in your table...
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim Found As Boolean
        If TextBox1.Text.Length.Equals(16) Then
            For i As Integer = 0 To Column1.DataGridView.Rows.Count - 4    'Why -4
                If Column1.DataGridView.Item(0, i).Value = TextBox1.Text Then
                    Found = True
                    My.Computer.Audio.Play("\\Stellent2\fin_wp\Groups\02-Public_IT\Shared_IT\Parvo_noise\beep.wav")
                    Dim dbcon As New SqlConnection("Data Source = PRIVATE
                    dbcon.Open()
                    Dim dbcmd As New SqlCommand
                    dbcmd.Connection = dbcon
                    dbcmd.CommandText = "Insert into Match_Table(Unit) Values ('" & TextBox1.Text & "')"
                    'dbcmd.CommandText = "Insert into Match_Table(COLLECT) = COLLECTDataGridViewTextBoxColumn"
                    dbcmd.ExecuteNonQuery()
                    'dbcmd.CommandText = "Insert into Match_Table(Center) Values ('" & LifeTec_EliteDataSet1.CICBC_PARVO_PROCEDURE.CENTERColumn.ToString & " ')"
                    MATCH_NOMATCHDataGrid.DataGridView.Item(3, i).Value = "MATCH"
                    ORDERDataGrid.DataGridView.Item(4, i).Value = TimeString
                    Me.TextBox1.Clear()
                    Exit For
                End If
            Next
            If Not Found Then
                My.Computer.Audio.Play("\\Stellent2\fin_wp\Groups\02-Public_IT\Shared_IT\Parvo_noise\nonono.wav")
            End If
            Me.TextBox1.Clear()
        End If
        Me.TextBox1.Focus()

    End Sub

Open in new window

Very easy to follow.. very quick and to follow.

Thank you for your assistance! :)
Very very welcome!!!