【问题标题】:Display Record According to Month按月显示记录
【发布时间】:2013-12-20 08:11:43
【问题描述】:

我有一个名为 PriceTesting 的数据库(使用 Microsoft Access 2007),其中包含一个名为 tbl_order 的表,其中包含以下列:

Order_ID, Customer_Name, Dress_Type, Dress_Price, Quantity, Date_Of_Pickup, Payment_Status

我已成功使用此代码将数据显示到 datagridview 中:-

 Private Sub dgvReportShow()

        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"

        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If

        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New OleDb.OleDbDataAdapter

        da = New OleDb.OleDbDataAdapter("SELECT Order_ID, Customer_Name, Dress_Type, Dress_Price, Quantity, Date_Of_Pickup, Payment_Status, Dress_Price * Quantity as Total " & _
                                        "FROM tbl_order " & _
                                        "WHERE (Payment_Status = 'paid') ", con)

        da.Fill(dt)

        dgvReport.DataSource = dt.DefaultView

        dgvReport.SelectionMode = DataGridViewSelectionMode.FullRowSelect

End Sub

Date_Of_Pickup 在datagridview中显示为28-Dec-13 (对不起...我没有足够的点来发布快照)

现在我添加了一个包含月份的comboboxMonth(一月、二月、三月……等等) 这样我就可以在comboboxMonth 中查看所选月份的记录

我如何将" 28-Dec-13 " 转换为月份以便我可以添加

 " WHERE (Payment_Status = 'paid') AND Date_Of_Pickup = comboboxMonth.value "

谁能帮我解决这个问题?

【问题讨论】:

  • 如何填充组合框?只是包含月份名称的字符串?
  • yup.. 我在表单设计中手动添加了月份
  • 那么组合的第一个索引是一月等月份?

标签: vb.net datagridview combobox ms-access-2007


【解决方案1】:

如果您使用升序的月份名称手动填充组合(一月一号,二月二号,依此类推,您可以在 SelectedIndexChanged 事件中编写类似这样的内容

Private Sub cbo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboBoxMonths.SelectedIndexChanged
    Dim cbo = CType(sender, ComboBox)
    Dim monthIndex = cbo.SelectedIndex
    if monthIndex <> -1 then
       Dim cmdText = "SELECT Order_ID, Customer_Name, Dress_Type, Dress_Price, Quantity, " & _
                     "Date_Of_Pickup, Payment_Status, Dress_Price * Quantity as Total " & _
                     "FROM tbl_order WHERE (Payment_Status = 'paid') AND " & _ 
                     "Month(Date_Of_Pickup) = " & (monthIndex + 1).ToString

      .......


    End if
End Sub

【讨论】:

  • aaaaahh.. 通过使用 selectedindex 呵呵.. 好吧我尝试实现这个...让你稍后知道结果..
  • 代码工作... tq 非常.. 但我不知道“Dim cbo = CType(sender, ComboBox)”这部分是干什么用的??
  • sender 作为对象传递给事件处理程序,但我们知道这是生成事件的组合框。如果我们想提取 SelectedIndex 属性,我们需要将其转换回适当的类型,这就是 CType 的工作.....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多