I need help upgrading a flexgrid to a data grid view

Posted on 2008-11-17
Medium Priority
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
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 22987881
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

ID: 22988034
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

ID: 22991836

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

Priest04 earned 500 total points
ID: 22993735
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

ID: 31517750
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

864 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