2011-07-10 72 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 & "'" 

端子

相關問題