从datagridview中的虚拟列获取警告 [英] Get warning from virtual column in datagridview

查看:85
本文介绍了从datagridview中的虚拟列获取警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含来自access数据库的数据的表单,其中包含一些字段,其中一个是字段日期,如何使用字段日期标准显示到数据网格,从当前日期起不到30天



我尝试过:



I created a form with data from acces database with some fields and one of them there is field date, how to display to datagrid with field date criterion less than 30 days from current date

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      'DataGridView2.DataSource.clear()

      Try
          If con.State = ConnectionState.Closed Then
              con.Open()
          End If
          sql = "Select *, datediff('d',Date(),thmasuk )as kurang from tpegawai"
          'sql = "select count(nip) as Expired from Tpegawai where datediff('d', Date(), thmasuk) and datediff('d', Date(), thmasuk)  & Text1.Text
          da = New OleDb.OleDbDataAdapter(sql, con)
          da.Fill(ds, "dbpegawai")
          con.Close()
          '  If IsNumeric(Text1.Text) And Text1.Text <> 0 Then
          ' add the limitation of date
          ' sql = sql & " where datediff('d', Date(), thmasuk) < " & Text1.Text
          ' End If
          '    sort by remaining date
          'sql = sql & " order by datediff('d', Date(), thmasuk) asc"
      Catch ex As Exception
          MessageBox.Show(ex.ToString)
      End Try
      DataGridView2.DataSource = ds.Tables("dbpegawai")

  End Sub

推荐答案

首先,你应该避免 SqlInjection [ ^ ]通过创建参数化查询。



假设您要显示与用户定义的日期范围相对应的数据(Text1.Text ),您可以尝试这样的事情:

First of all, you should avoid SqlInjection[^] by creating parameterized queries.

Assuming, that you'd like to display data corresponding to date range defined by the user (Text1.Text), you can try something like this:
sql = String.Format("SELECT <Field_List> {0} FROM Tpegawai {0} WHERE DateDiff('d', Date(), thmasuk)<=?", Environment.NewLine)
con.Open()
Dim dt As DataTable = New DataTable()
Using command As OleDbCommand = New OleDbCommand(sql, con)
    command.Parameters.Add(Me.Text1.Text)
    Dim oRdr As OleDbDataReader = command.ExecuteNonQuery()
    dt.Load(oRdr)
    oRdr.Dispose()
    command.Dispose()
End Using
con.Close()
'dt holds data; you can bind it with DataGridView.DataSource
DataGridView1.DataSource1 = dt





欲了解更多详情,请参阅:

OleDbCommand.Parameters属性(System.Data.OleDb) [ ^ ]


这篇关于从datagridview中的虚拟列获取警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆