本文介绍了数据未加载到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击按钮没有在datagridview中显示数据时,尊重的数据在数据库中可用

请帮助我;

when click the button didn't show a data in datagridview, the respected data is available in database
Please help me;

Private Sub load_acc()
       Try
            sqL = "SELECT DISTINCT [POSNo], [SubTotal], [Discount], [VAT], [Total], [Type] FROM tblPOS WHERE POSDate >= #" & dtpFrom.Text & "# AND POSDate <=#" & dtpTo.Text & "# ORDER BY POSNo"

           ConnDB()
           cmd = New OleDbCommand(sqL, conn)
           dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
           dgw.Rows.Clear()

           subtotal = 0
           disscount = 0
           tax = 0
           totalAmount = 0

           Do While dr.Read = True
               dgw.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4), dr(5))
               subtotal += dr(1)
               'disscount += dr(2)
               ' tax += dr(3)
               totalAmount += dr(4)
           Loop

           lblSubTotal.Text = Format(subtotal, "#,##0.00")
           lblDiscount.Text = Format(disscount, "#,##0.00")
           lblTAX.Text = Format(tax, "#,##0.00")
           lblTotal.Text = Format(totalAmount, "#,##0.00")
       Catch ex As Exception
           MsgBox(ex.Message)
       Finally
           cmd.Dispose()
           conn.Close()
       End Try
   End Sub

推荐答案

dgw.DataSource = null
dgw.Columns.Clear()
dgw.AutoGenerateColumns = false



然后添加你需要的列,通常你只需要DataGridViewTextBoxColumns,但通过添加它们,你可以设置数据类型和显示格式,这样你就可以正确地对它们进行排序。设置列可见性,宽度和标题等属性。





其次;将DataReader结果加载到DataTable,然后将DataTable绑定到DataGridView - 请参阅下文;


Then add in the columns you require, typically you only need DataGridViewTextBoxColumns but by adding them you get to set the data-type and display format which allows you to sort them correctly & set properties like column visibility, widths and headings.
Refer here for MSDN documentation on the DataGridViewTextBoxColumn

Secondly; Load the DataReader results to a DataTable and then bind the DataTable to the DataGridView - refer below;

Dim dataTable as DataTable
dataTable.Load(dr)
dgw.DataSource = dataTable





第三名;使用数据库上的表达式重新使用数据表来获取所需的标签值





这将允许您获取摆脱逐行处理数据会更有效率



亲切的问候



Third; Re-use the datatable to get the label values you require using an Expression on a datacolumn
Refer here for MSDN documentation on DataColumn.Expression

This will allow you to get rid handling the data row by row & will be much more efficient

Kind Regards


这篇关于数据未加载到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:25