I need help upgrading a flexgrid to a data grid view

Posted on 2008-11-17
Last Modified: 2012-05-05
I need help changing the code from the VB6 code attached for a flexgrid to .net 2005 data grid view.
Code would be nice!!

Thanks in advance,


        With MSFlexTXDetail

            .Cols = 5

            .Rows = 2

            .TextMatrix(0, 1) = "ID"

            .TextMatrix(0, 2) = "Description"

            .TextMatrix(0, 3) = "Quantity"

            .TextMatrix(0, 4) = "Item Price"


            .ColAlignment(1) = flexAlignLeftBottom

            .ColAlignment(2) = flexAlignLeftBottom

            .ColAlignment(3) = flexAlignLeftBottom

            .ColAlignment(4) = flexAlignRightBottom

            .ColWidth(1) = 0

            .ColWidth(2) = 4500

            .ColWidth(3) = 1000

            .ColWidth(4) = 2000

        End With

        i = 1


       If rsTXDetail.RecordCount > 0 Then

          mcurTaxable = 0

          mcurTaxable2 = 0

            Do Until rsTXDetail.EOF

                  strDesc = Format(Trim(rsTXDetail.Fields("Desc")))

                  intQuantity = Format(Trim(rsTXDetail.Fields("Quan")))

                  curItemPrice = Trim(rsTXDetail.Fields("ItemPrice"))

                  If rsTXDetail.Fields("TAX") = 1 Then

                      mcurTaxable = mcurTaxable + curItemPrice

                      mcurTax = (mcurTaxable * gintTaxRate)

                  ElseIf rsTXDetail.Fields("TAX") = 2 Then

                      mcurTaxable2 = mcurTaxable2 + curItemPrice

                      mcurTax2 = (mcurTaxable2 * gintTaxRate2)

                  End If

                  gcurTax1 = mcurTaxable

                  gcurTax2 = mcurTaxable2

                  mcurSubtotal = mcurSubtotal + Trim(rsTXDetail.Fields("ItemPrice"))

            With MSFlexTXDetail

                .AddItem ""

                .TextMatrix(i, 1) = rsTXDetail!ID

                .TextMatrix(i, 2) = strDesc

                .TextMatrix(i, 3) = intQuantity

                .TextMatrix(i, 4) = Format(curItemPrice, "$##,###.00")

            End With

            intItems = intItems + 1

Open in new window

Question by:JDL129
    LVL 96

    Expert Comment

    by:Bob Learned
    1) Drop a DataGridView on a form

    2) Create or fill a DataTable.

    3) Set the DataGridView.DataSource to the DataTable.

    4) Define columns through the DataGridView designer, or in code.

    5) Set row/column/cell styles.
    LVL 18

    Expert Comment

    as example I am using DataTable dtTXDetail instead of rsTXDetail, since in .NET you cannot use ADO objects (at least not recommended).
            If MSFlexTXDetail.ColumnCount = 0 Then
                With MSFlexTXDetail
                    .Columns.Add("cID", "ID")
                    .Columns.Add("cDescription", "Description")
                    .Columns.Add("cQuantity", "Quantity")
                    .Columns.Add("cItemPrice", "Item Price")
                    .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
                    .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
                    .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
                    .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
                    .Columns(0).Width = 0
                    .Columns(1).Width = 4500
                    .Columns(2).Width = 1000
                    .Columns(3).Width = 2000
                    .RowCount = 2
                End With
            End If
            Dim dtTXDetail As New DataTable()
            If dtTXDetail.Rows.Count > 0 Then
                Dim mcurTaxable As Double = 0
                Dim mcurTaxable2 As Double = 0
                For Each dr As DataRow In dtTXDetail.Rows
                    Dim strDesc As String = Trim(dr("Desc"))
                    Dim intQuantity As Integer = dr("Quan")
                    Dim curItemPrice As Double = dr("ItemPrice")
                    Select Case dr("TAX")
                        Case 1
                            mcurTaxable = mcurTaxable + curItemPrice
                            mcurTax = (mcurTaxable * gintTaxRate)
                        Case 2
                            mcurTaxable2 = mcurTaxable2 + curItemPrice
                            mcurTax2 = (mcurTaxable2 * gintTaxRate2)
                    End Select
                    gcurTax1 = mcurTaxable
                    gcurTax2 = mcurTaxable2
                    mcurSubtotal += dr("ItemPrice")
                    With MSFlexTXDetail
                        .Rows.Add(dr("ID"), strDesc, intQuantity, Format(curItemPrice, "$##,###.00"))
                    End With
                intItems = dtTXDetail.Rows.Count
            End If

    Open in new window


    Author Comment


    Thanks for your post!!
    My data source Is:"Select * from tblTXDetail where TXNum = " & Val(me.lblTXNum.text) & " category <> 'TAX' order by ID Desc."
    How would one select the data source with the data table?

    Each of the three variables has an error message: "Variable strDesc hides a variable in an enclosing block"
    Any Ideas?

    Thanks again for your help!!

    LVL 18

    Accepted Solution

    JDL129, we wont be written the whole program her, would we? :) YOu need to read some basic info about how ADO.NET works if you think of doing some database programming.

    Your data source cannot be the query, your data source (DBMS) can be SQL server, Orable, MySql, Access database, etc. Based on the DBMS you will choose appropriate objects. I will post bellow the example for MSSQL server (if using Access, you will use OleDbDataAdapter instead). Aslo note that you need a valid Sql connection (or OleDbConnection, if using MS Access).

    Considering the exception you are receiving, it means that some variable is declared twice. Check your code to see which variable is that.

    Dim da As New SqlDataAdapter("Select * from tblTXDetail where TXNum = " & Val(me.lblTXNum.text) & " category <> 'TAX' order by ID Desc.", sqlConnection)
    dim dtTXDetail As new DataTable();

    Open in new window


    Author Closing Comment

    Nope, not writing the whole program.  I am in the process of converting a vb6 app into .net and am running into a couple of learning curves.

    Thanks for your post,


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now