我搜索过此内容但找不到gridview外部textchanged
事件的任何解决方案,导致结果发生变化。我无法附加图片作为新手。我希望通过数据表在gridview中显示的结果根据我在gridview外部放置的文本框中键入的内容进行更新。 c#中的代码将不胜感激。最后,我想按ENTER键检索ID以进一步使用。
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
string sql; table.Columns.Add("Name");
dbconnect db = new dbconnect();
db.createconnection();
sql = "select custcompany from customer";
db.dbadapter = new OleDbDataAdapter(sql, db.dbconnection);
DataTable dtt = new DataTable();
db.dbadapter.Fill(dtt);
for (int i = 0; i < dtt.Rows.Count; i++)
{
table.Rows.Add(dtt.Rows[i][0]);
}
sql = "select suppcompany from supplier";
db.dbadapter = new OleDbDataAdapter(sql, db.dbconnection);
DataTable dtt1 = new DataTable();
db.dbadapter.Fill(dtt1);
for (int i = 0; i < dtt1.Rows.Count; i++)
{
table.Rows.Add(dtt1.Rows[i][0]);
}
dataGridView1.DataSource = table;
if (textBox1.Text != "")
{
DataView dv = new DataView();
dv.RowFilter = string.Format("Name = '{0}'", textBox1.Text);
dataGridView1.DataSource = dv;
}
else if (textBox1.Text == "")
{
dataGridView1.DataSource = table;
}
}
table
是在Load事件上填充的DataTable的名称。
答案 0 :(得分:2)
如果测试框有值,您正在创建DataView
的新实例并将其分配给dataGridView1
,我的问题是 DataView对象中是否有值?如果你正在完成你在问题中提供的内容然后它将无效,因为dv
没有值显示在dataGridView1
。
通过访问数据库获取table
事件的TextChanged
数据。如果您有任何代码将数据库值输入table
,那么在绑定到DataGridView1
之前在此事件中使用它。
因为table
后PostBack
值会丢失。因此,您需要再次将值重新加载到table
。请尝试以下方法,
private void textBox1_TextChanged(object sender, EventArgs e)
{
//table = fill the table with proper values by accessing the database
if (textBox1.Text != "")
{
DataView dv = table.DefaultView;
dv.RowFilter = string.Format("Name = '{0}'", textBox1.Text);
dataGridView1.DataSource = dv;
dataGridView1.DataBind();
}
else if (textBox1.Text == "")
{
dataGridView1.DataSource = table;
dataGridView1.DataBind();
}
}
<强>更新强>
要搜索以字母开头的名称,
dv.RowFilter = string.Format("Name like '{0}%'", textBox1.Text);
要搜索包含字母的名称,
dv.RowFilter = string.Format("Name like '%{0}%'", textBox1.Text);
要搜索以字母结尾的名称,
dv.RowFilter = string.Format("Name like '%{0}'", textBox1.Text);