2011-07-10 71 views
2

我有一个Form和一个本地数据库和Table1DataGridView。我也有2 DatetimePickers和一个按钮。如何显示两个日期之间的所有记录(VB.Net)

enter image description here

Date列定义为DateTime数据类型。

如何通过按button1来显示DataGridView中两个日期之间的记录?

我想与DataGridView Task做到这一点 - >Add Query... - >Query Builder...

如果这不是简单的,任何解决方案是值得欢迎的。

回答

3

这是一个解决方案(由我和谷歌搜索)。

这是简单的方法。

In DataGridView - >Add Query... - >Query Builder..。 在Date列(或任何你有)Filter单元格写:BETWEEN @ Date1 AND @ Date2。 单击确定,然后再次单击确定。

Form区域您现在看到一个带有2个文本框的Form标题区下的新行。首先是Date1,第二个是Date2。文本框区域后是一个名为Fillby的按钮。 将Date1,Date2和Fillby的名称从属性更改为任何您喜欢的内容,例如从: - 到: - 搜索。

要做到这一点的另一种方法比较困难。

首先把2 buttons和2 TimepickersForm地区。

Next in DataGridView - >Add Query... - >Query Builder..。 在Date列(或任何你有)Filter单元格写:BETWEEN @ Date1 AND @ Date2。 单击确定,然后再次单击确定。

Form代码视图区域粘贴第一个按钮代码(按钮1):

Public Function GetFromDate(ByVal value As DateTime) As DateTime 
     Return New DateTime(value.Year, value.Month, value.Day, 0, 0, 0, 0) 
    End Function 

    Public Function GetToDate(ByVal value As DateTime) As DateTime 
     Return New DateTime(value.Year, value.Month, value.Day, 23, 59, 59, 999) 
    End Function 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim BeginningDateTime As Date = GetFromDate(DateTimePicker1.Value) 
     Dim EndingDateTime As Date = GetToDate(DateTimePicker2.Value) 
     Try 
      Me.Table2TableAdapter.FillBy(Me.Database1DataSet1.Table2, New System.Nullable(Of Date)(CType(BeginningDateTime, Date)), New System.Nullable(Of Date)(CType(EndingDateTime, Date))) 
    Catch ex As System.Exception 
     System.Windows.Forms.MessageBox.Show(ex.Message) 
    End Try 
    End Sub 

而接下来这段代码的第二个按钮(Button2的):

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     Me.Table2TableAdapter.Fill(Me.Database1DataSet1.Table2) 
    End Sub 

你必须改变:

1)Table2TableAdapter什么是您的表适配器。

2)Database1DataSet1什么是您的数据库数据集。

3)Table2在什么是你的表。

代码FillBy,DateTimePicker1,DateTimePicker2如果您的数据库有差异,则必须更改。

有关代码的更多信息,您可以从查询构建器中看到代码。 它看起来像:

Private Sub FillByToolStripButton_Click... 

在那里,你可以找到所有信息的更改上面的代码。

毕竟你可以删除这段代码(Private Sub FillByToolStripButton_Click...)。

这就是我所找到的,我解决了我在两个日期之间搜索的问题。

1

最简单的和最简单的方法...

私人小组的button1_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)把手Button1.Click

'Filter the Connections Date wise 
    Me.TblUserBindingSource.Filter = "doj >= '" & DateTimePicker1.Value & "' and doj <= '" & DateTimePicker2.Value & "'" 

端子

相关问题