Format DataGridView Column - Code39 Barcode

Yanıt Format DataGridView Column - Code39 Barcode

  • 12 Nisan 2012 Perşembe 10:41
     
     

    Hello,

    I want to use barcodes in to my application in VB 2010. Also, I am a beginner in VB programming.

    I made a simple application. I fill a DataGridView form an SQL Table. After that i must apply a filter, and then print the form.

    I managed to filter the data from that DGV, but now i'm stuck at making that column to show barcodes instead of ID number.

    In the DataGridView, in the first column of the table, it’s the ID column. In SQL this colum is set as “int”

    In this column are unique ID Number, and I want to convert those numbers into barcodes (Code39 - I have this font installed).  

    I set the font of that column to be Code39 font, but this barcode needs  * symbol before and after the ID number, like this: *1234*  , that it can be read afterwards by a barcode scanner.

    How can I put those * in the ID Column ? It can be done adding a CellFormating Event?

    Thank


    • Düzenleyen SparX23 12 Nisan 2012 Perşembe 10:42
    •  

Tüm Yanıtlar

  • 12 Nisan 2012 Perşembe 11:20
     
     Yanıt Kod İçerir

    Hello,

    One suggestion would be to create a new column that uses an Expression as shown below. The column ID (which you mention as the first column) would not be shown in the DataGridView but instead the column with the expression.

    Also for consideration, if the column in question is the primary key (as you indicated is unique) you could set the DataTable as the DataSource of a BindingSource component which in turn becomes the DataSource for the DataGridView. Then at any point get the ID value if that needed from the BindingSource. Generally speaking the BindingSource would be declared at form level with WithEvents rather than Private so you have access to the event PositionChanged. If you need more on this let me know.

    dc = New DataColumn("MyBarCodeColumn")
    dc.DataType = GetType(String)
    dc.Expression = "'*' + ID + '*'"
    ds.Tables("YourTableName").Columns.Add(dc)


    KSG

    • Yanıt Olarak Öneren Nico Boey 12 Nisan 2012 Perşembe 14:47
    • Yanıt Olarak İşaretleyen SparX23 17 Nisan 2012 Salı 10:15
    •  
  • 12 Nisan 2012 Perşembe 13:33
     
     

    Thanks for your answer in such a short notice!

    It works fine :)

    I have another newbie question though. Can i manage to insert a expression to a column from right clicking on DataGridView -> Edit Columns?

    Thanks!

  • 12 Nisan 2012 Perşembe 14:38
     
     

    Thanks for your answer in such a short notice!

    It works fine :)

    I have another newbie question though. Can i manage to insert a expression to a column from right clicking on DataGridView -> Edit Columns?

    Thanks!

    Good to hear that worked for you. In regards to doing this in editing columns, no because this is done at the DataTable not the DataGridView but you can at the edit columns assign the Data property to the expression column under Data -> DataPropertyName.

    KSG

  • 13 Nisan 2012 Cuma 06:23
     
     

    Good to hear that worked for you. In regards to doing this in editing columns, no because this is done at the DataTable not the DataGridView but you can at the edit columns assign the Data property to the expression column under Data -> DataPropertyName.


    KSG

    Thanks again :). I forgot to tell you in the first post, that ID colum is the primary key, but you figure that out, by yourself :).

    Besides the unique ID, i need to make another column with the "status" of that ID. Can i use a barcode image, instead using Code39 Font?

    Can i add another column with expression that shows pictures in DataGridView based on other columns value?

    Something like:

    If Column "one" has value "ok" then column "two" shows picture "ok.bmp" (or other type of image like .jpg, .gif )

    If Column "one" has value "nok" then column "two" shows picture "nok.bmp"

  • 13 Nisan 2012 Cuma 06:44
     
      Kod İçerir

    Good to hear that worked for you. In regards to doing this in editing columns, no because this is done at the DataTable not the DataGridView but you can at the edit columns assign the Data property to the expression column under Data -> DataPropertyName.


    KSG

    Thanks again :). I forgot to tell you in the first post, that ID colum is the primary key, but you figure that out, by yourself :).

    Besides the unique ID, i need to make another column with the "status" of that ID. Can i use a barcode image, instead using Code39 Font?

    Can i add another column with expression that shows pictures in DataGridView based on other columns value?

    Something like:

    If Column "one" has value "ok" then column "two" shows picture "ok.bmp" (or other type of image like .jpg, .gif )

    If Column "one" has value "nok" then column "two" shows picture "nok.bmp"

    If you have a set of images to show i.e. ok.bmp and nok.bmp they could be added as resources to your project and the column which they would be assigned to would be done something like

    dt.Columns.Add("CompanyStatusImage", System.Type.GetType("System.Byte[]"))

    Then in an event perhaps in CellFormatting of the DataGridView or a event of the DataTable assign an image based on a dynamic condition. If you want an example of how this can be done (let's call it a proof of concept) go to the following link below which has a demo for VS2008 and VS2005.

    http://www.vbforums.com/showthread.php?t=636668

    Please note that in these example the image is changed in a button click event but as mentioned above can be done in one of the events of the DataTable or DataGridView CellFormatting.

    For a crash course in DataTable events (which is over the top as it was to teach about these events) download a sample project here


    KSG

  • 13 Nisan 2012 Cuma 06:58
     
     

    Thanks a lot for helping me and providing me those links :)

    I have those .bmp files in the "Resources" folder in my project.

    I will go now, and try to make this work. I'll give you a feedback asap :)

    Thanks again :)

  • 17 Nisan 2012 Salı 09:54
     
      Kod İçerir

    Hello Hevin,

    I hit another bump with my project. It's about printing the form. I try printing the form with Visual Basic PowerPacks - PrintForm. Everything was smooth until I saw the printed paper. Printing quality is very low, and the barcode can't be read with the barcode scanner. 

    I have 2 Datagridviews in my form, and I would like to print the form as it is, if that is possible, but at better quality. 

    I tried diffrent ways to print that form with no succes. What do you suggest ?

    As i said, I have 2 datagridviews and 4 buttons (one for filtering called "TestOrder", one for Printing called "btnPrint", one for print preview called "btnPrintPreview", and one for page setup called "btnPageSetup").

    This is the base code in my form:

    Public Class PrintTO
        Private Sub PrintTO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SingleTestTableAdapter.Fill(Me.TestdbDataSet.SingleTest)
            Me.ERO_TO_HeaderTableAdapter.Fill(Me.TestdbDataSet.ERO_TO_Header)
        End Sub
        Private Sub ButtonSelectTO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelectTO.Click
            ERO_TO_HeaderBindingSource.Filter = "[ERO_TO] = '" & TestOrder.Text & "'"
        End Sub
        Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
           	PageSetupDialog1.ShowDialog()
        End Sub
        Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
            PrintPreviewDialog1.ShowDialog()
        End Sub
        Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            
        End Sub

    Greetings,

    I would suggest starting a new thread for your current question and reference back to this thread so others completely understand everything. This will give more visibility to the current question.


    KSG

  • 19 Nisan 2012 Perşembe 05:51
     
      Kod İçerir

    Hello Kevin,

    I'm having a hard time understanding how to format the cells of the DGV image column.

    I created a new Column in DataGridView (ColumnType = DataGridViewImageColumn), and I tried to make a CellFormating event, but with no succes.

    I tried this but it didn't work:

    Private Sub GridView_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView.CellFormatting
    If DataGridView(2, e.RowIndex).Value = "ok" Then
                DataGridView(3, e.RowIndex).Value = My.Resources.ResourceManager.GetObject("ok.bmp")
    ElseIf DataGridView(2, e.RowIndex).Value = "nok" Then
                DataGridView(3, e.RowIndex).Value = My.Resources.ResourceManager.GetObject("nok.bmp")
    End If
    End Sub

    What is wrong with this code? Any suggestion ?

    I took a look at the example you provided, but I want my DGV Column3 (the image column)  to change image dynamically based on Column2 (text column)

    What about if i want to use a Expression in DataTable in "Symbol" Column, how that expression looks like ? 

    • Düzenleyen SparX23 19 Nisan 2012 Perşembe 07:08
    •  
  • 19 Nisan 2012 Perşembe 15:29
     
      Kod İçerir

    Hello,

    Download the following project and replace the code found in the form with the code below. Build/Run the project. Once the project is running change in the second column a cell to and empty value, press ENTER. Next change a value in the second column to HELLO and press ENTER. This demos dynamically changing an image in the first column.

    Public Class frmMainForm
        WithEvents bsData As New BindingSource
        Private MyIcons As New DataGridViewIcons
        Private Identifier As Integer = 0
        Private Sub Form1_Load() Handles MyBase.Load
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.AllowUserToAddRows = False
            Dim dt As New DataTable
            dt = SimulateLoadingTableFromDatabase()
            dt.Columns.Add("CompanyStatusImage", System.Type.GetType("System.Byte[]"))
            For Each row As DataRow In dt.Rows
                DoIconImages(row, MyIcons)
            Next
            bsData.DataSource = dt
            DataGridView1.DataSource = bsData
            ListBox1.SelectedIndex = 3
        End Sub
        ''' <summary>
        ''' Simulate editing the current row and updating the icon
        ''' </summary>
        ''' <remarks>
        ''' There are several different ways to do the update, this is but one.
        ''' </remarks>
        Private Sub cmdChange_Click() Handles cmdChange.Click
            Dim Status As Int32
            Select Case ListBox1.Text
                Case "Editing"
                    Status = 10
                Case "Rejected"
                    Status = 20
                Case "SentForProofing"
                    Status = 30
                Case "OpenedForProofing"
                    Status = 40
                Case "Approved"
                    Status = 50
            End Select
            Dim CurrentRow As DataRow
            CurrentRow = bsData.DataTable.Select("ID=" & bsData.CurrentRowIdentifier)(0)
            CurrentRow("Status") = Status
            DoIconImages(CurrentRow, MyIcons)
        End Sub
        Private Sub bsData_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsData.PositionChanged
            Identifier = CInt(CType(bsData.Current, DataRowView).Row("ID"))
        End Sub
        ''' <summary>
        ''' For simulations empty a company name or change company name
        ''' value to HELLO.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            If bsData.Current Is Nothing Then
                Exit Sub
            End If
            Dim Status As Int32 = 10
            Dim CurrentRow As DataRow
            CurrentRow = bsData.DataTable.Select("ID=" & bsData.CurrentRowIdentifier)(0)
            If CType(bsData.Current, DataRowView).Row("Company_Name").ToString = "" Then
                Select Case ListBox1.Text
                    Case "Editing"
                        Status = 10
                    Case "Rejected"
                        Status = 20
                    Case "SentForProofing"
                        Status = 30
                    Case "OpenedForProofing"
                        Status = 40
                    Case "Approved"
                        Status = 50
                End Select
                CurrentRow("Status") = Status
                DoIconImages(CurrentRow, MyIcons)
            ElseIf CType(bsData.Current, DataRowView).Row("Company_Name").ToString = "HELLO" Then
                CurrentRow("Status") = Status
                DoIconImages(CurrentRow, MyIcons)
            End If
        End Sub
    End Class

    Hope this helps.

    KSG