C# 移动到dataGridView中的下一行
我正在尝试开发一个表单,它将在数据库中搜索输入的名称。保存所选行,然后移动到下一行。但当我再次搜索时,它会清除以前保存的行C# 移动到dataGridView中的下一行,c#,database,datagridview,oledb,C#,Database,Datagridview,Oledb,我正在尝试开发一个表单,它将在数据库中搜索输入的名称。保存所选行,然后移动到下一行。但当我再次搜索时,它会清除以前保存的行 private void textBox1_TextChanged(object sender, EventArgs e) { DataView DV = new DataView(datatable); DV.RowFilter = string.Format("proName LIKE '%{0}%'", textBox1.Tex
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;
}
}
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
DataView DV=新的DataView(datatable);
DV.RowFilter=string.Format(“类似“{0}%”的proName”,textBox1.Text);
dataGridView1.DataSource=DV;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
OleDbCommand命令=新的OleDbCommand(“从productDetails中选择*”,连接);
数据集=新数据集();
OleDbDataAdapter dataAdapter=新的OleDbDataAdapter();
connection.ConnectionString=@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\Documents and Settings\Zimad\Desktop\project1.accdb”;
connection.Open();
dataAdapter.SelectCommand=命令;
dataAdapter.Fill(datatable);
dataGridView1.DataSource=datatable;
connection.Close();
nRow=dataGridView1.CurrentCell.RowIndex;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(nRow
按钮1用于保存所选行。@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();
}
您正在使用
dataGridView1.datasource=DV重置数据源代码>,因此执行此操作时会丢失以前的所有选择。您必须存储选择信息,并在调用后将其放回,这正是我无法理解的部分。我该如何存储它以及如何调用它?可能对每个选定的行使用某种键,然后在更新数据源时,在行中循环查找匹配的键。为什么不能直接从数据库中搜索。在加载表单时不加载所有行。然后,您可以在datagridview中仅显示由搜索文本创建的行