我正在尝试开发一个表单,用于在数据库中搜索输入的名称。保存选定的行,然后移动到下一行。但是当我再次搜索时,它会清除以前保存的行。
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataView DV = new DataView(datatable);
DV.RowFilter = string.Format("proName LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = DV;
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand("select * FROM productDetails",connection);
DataSet dataset = new DataSet();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Zimad\Desktop\project1.accdb";
connection.Open();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(datatable);
dataGridView1.DataSource = datatable;
connection.Close();
nRow = dataGridView1.CurrentCell.RowIndex;
}
private void button1_Click(object sender, EventArgs e)
{
if (nRow < dataGridView1.RowCount)
{
dataGridView1.Rows[nRow].Selected = false;
dataGridView1.Rows[++nRow].Selected = true;
}
}
button1用于保存选定的行。
答案 0 :(得分:0)
@Fabio是对的。我在这里做的是我将搜索结果加载到文本框中,然后在dataGridView
中加载项目的信息 private void textBox1_TextChanged(object sender, EventArgs e)
{
listBox1.Visible = true;
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * FROM ProductDetails";
OleDbDataReader reader = command.ExecuteReader();
listBox1.Items.Clear();
while (reader.Read())
{
if (reader["proName"].ToString().Contains(textBox1.Text))
{
listBox1.Items.Add(reader["proName"].ToString());
}
}
connection.Close();
}
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * FROM ProductDetails";
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (reader["proName"].ToString().Contains(textBox1.Text))
{
dataGridView1.Rows.Add();
dataGridView1[0, 0].Value = reader["proName"].ToString();
}
}
connection.Close();
}