Showing posts with label RowFilter. Show all posts
Showing posts with label RowFilter. Show all posts

Monday, January 16, 2012

RowFilter สำหรับ Date

กรณีใช้ RowFilter สำหรับคอลัมน์ชนิด Text ธรรมดา ก็ไม่ค่อยมีปัญหาอะไรหรอกแต่พอมา Filter คอลัมน์ที่เป็นวันที่หนะสิ ผลมันไม่ออกมาอย่างที่ต้องการและมักมี Error ด้วย ก็เลยลองสั่งจนได้ผลที่ต้องการมีตัวอย่างดังนี้

Dim d1, d2 As Date
d1 = New DateTime(DTP1.Value.Year, DTP1.Value.Month, DTP1.Value.Day, 8, 0, 0)
d2 = New DateTime(DTP2.Value.Year, DTP2.Value.Month, DTP2.Value.Day, 8, 0, 0)
d2 = d2.AddDays(1)
Dim d1text, d2text As String
d1text = d1.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.CreateSpecificCulture("en-GB"))
d2text = d2.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.CreateSpecificCulture("en-GB"))
Dim dView As DataView
dView = dtDownTime.DefaultView
Dim sFilter As String = ""
sFilter = "REPORTED_BY like 'P%' "
sFilter &= "AND REQUIRED_START_DATE >= #" & d1text & "#"
dView.RowFilter = sFilter
dgvDowntime.DataSource = dView
formatDgvDownTime()