我试图通过从其组合框项目中搜索文本框中的关键字来过滤我的datagridview,其中source是我的表中的列名。我不知道如何在c#中做到这一点。
我所知道的就是这个vb.net代码:
Private Sub txtkeyword_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtkeyword.TextChanged
Dim qry As String
If cmbfilter.Text = "EMPLOYEE_ID" Then
qry = "select * from tblpayslip where EMPLOYEE_ID like '%" & txtkeyword.Text & "%'"
Else
qry = "select * from tblemployee"
End If
connect()
conn.Open()
cmd = conn.CreateCommand
cmd.CommandText = qry
dr = cmd.ExecuteReader
If dr.HasRows Then
Dim dtpayslip As New DataTable
dtpayslip.Load(dr)
dgvpayslip.DataSource = dtpayslip
End If
dgvpayslip.Columns(0).HeaderCell.Value = "EMPLOYEE_ID"
dgvpayslip.Columns(1).HeaderCell.Value = "SALARY_PER_DAY"
dgvpayslip.Columns(2).HeaderCell.Value = "NO_OF_DAYS_WORKED"
dgvpayslip.Columns(3).HeaderCell.Value = "GROSS_PAY"
dgvpayslip.Columns(4).HeaderCell.Value = "SSS"
dgvpayslip.Columns(5).HeaderCell.Value = "PAG_IBIG"
dgvpayslip.Columns(6).HeaderCell.Value = "TAX"
dgvpayslip.Columns(7).HeaderCell.Value = "NET_PAY"
dgvpayslip.ClearSelection()
End Sub
答案 0 :(得分:0)
你并不总是需要使用缺乏性能的SQL,可能你可以在form_load事件上填充数据表并在textchange()事件上设置rowfilter。
按照下面的textchange事件代码,将您的SQL功能移到form_load事件
if (!string.IsNullOrEmpty(comboBox1.Text))
{
var dt = (DataTable)dgvpayslip.DataSource;
dt.DefaultView.RowFilter = string.Format("{0} like '%{1}%'",comboBox1.Text.Trim().Replace("'", "''"), txtkeyword.Text.Trim().Replace("'", "''"));
dgvpayslip.Refresh();
}