我不是编程新手,但我刚刚进入c#并编写我的第一个程序。
我的目标是一个资产事件跟踪系统,基本而且恰到好处我们需要的东西,仅此而已。我从小做起,然后继续努力。
到目前为止,我已成功连接并查询MS Access数据库并将表投影到DataGridView对象中。
我也成功地过滤了结果,但我现在正试图继续前进。我的列的名称不是我想要显示为DataGridView中的列标题,因此我修改了我的查询以包含as语法。
SELECT serialnumber as `Serial Number`, owner as `Owner`, datesubmitted as `Date Submitted`,dateprocessed as `Date Processed`,technician as `Technician`,status as `Status`,datereturned as `Date Returned`,problem as `Problem` from incidents
因此,我过滤的方式是使用下拉对象选择列,使用文本框搜索所需列中的值。我在下拉列表中手动输入了这些项目,因此它们不会动态填充。在我的' AS'之前语法,我正在使用if / else if if将我漂亮的下拉文本转换为实际的列名并且有效。在我更改查询后,我收到了列未找到的错误,因此我在if / else if块中更改了我的语法,以搜索“漂亮”的错误。列名,不要去。
然后我想,一起摆脱它们并提出这个:
dv.RowFilter = string.Format("'{0}' LIKE '%{1}%'", searchCriteria.Text, textSearch.Text);
任何人都可以帮助或指出我正确的方向吗?我很难找到或者甚至只是理解我遇到的搜索结果。
到目前为止,这是整个计划。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IncidentTracking
{
public partial class Form1 : Form
{
private DataTable dt;
private OleDbConnection connString;
private OleDbCommand command;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'assetTrackingDataSet.incidents' table. You can move, or remove it, as needed.
this.incidentsTableAdapter.Fill(this.assetTrackingDataSet.incidents);
connString = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\AssetTracking.accdb");
command = new OleDbCommand("SELECT serialnumber as `Serial Number`, owner as `Owner`, datesubmitted as `Date Submitted`,dateprocessed as `Date Processed`,technician as `Technician`,status as `Status`,datereturned as `Date Returned`,problem as `Problem` from incidents", connString);
connString.Open();
using (OleDbDataAdapter da = new OleDbDataAdapter(command))
{
dt = new DataTable("incidents");
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
private void textSearch_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("'{0}' LIKE '%{1}%'", searchCriteria.Text, textSearch.Text);
if (textSearch.Text == "")
{
dv.RowFilter = null;
}
dataGridView1.DataSource = dv.ToTable();
}
}
}