如何使用Access数据库从C#中的datagridview打印选定的行 [英] How to print selected row from datagridview in C# with access database
本文介绍了如何使用Access数据库从C#中的datagridview打印选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我使用这些链接编写了以下代码:
Hi all,
I used those link to write the following code:
private void search_Click(object sender, EventArgs e)
{
string queryString = "SELECT * FROM account WHERE id='1' ";
OleDbCommand command = new OleDbCommand(queryString, vcon);
//command.Parameters.Add("id", OleDbType.Char, 3).Value = "1";
OleDbDataReader reader = command.ExecuteReader();
}
但是这些代码无法帮助我在datagridview中显示选定的行.
也许我不了解你.让我详细了解您.
我还通过以下代码在datagridview中搜索了所有数据:
But These code can''t help me to display selected row in datagridview.
Maybe I''m don''t understand you. Let me details to know you.
I had also performed search all data in datagridview by the following code:
private void button10_Click(object sender, EventArgs e)
{
string vsql = "select * from account";
OleDbCommand vcom = new OleDbCommand(vsql, vcon);
DataSet vds = new DataSet();
OleDbDataAdapter vda = new OleDbDataAdapter(vcom);
vda.Fill(vds, "res");
dataGridView1.DataSource = vds.Tables["res"];
vda.Dispose();
vcom.Dispose();
}
但是我无法通过上面的第一个代码来执行搜索所选行的操作.
请帮助我.
But I can''t preform to search selected row by the first above following code.
please help me.
推荐答案
如果是表格,请查看以下简短说明: ^ ]
将来,请勿在SQL语句中使用文字(1),而应使用 ^ ]
If this is forms, have a look at this brief description: How to: Bind Data to the Windows Forms DataGridView Control[^]
For the future, don''t use literals (1) in your SQL statements, instead use SqlParameter[^]
检查是否有帮助,仅首先显示GridView中的行 [ ^ ]
Check if this helps, Only display first row in GridView[^]
最后,我得到一个解决方案,该方法如何在listview或datagridview中显示选定的行数据.在这里,我使用listview来显示,并使用一个文本框将数据与Access数据库进行比较,并使用一个按钮进行搜索.
At last I get a solution how to display selected row data in listview or datagridview. Here I used listview to display and used a textbox for compare data with access database and also a button to search.
private void button_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter("select * from table", vcon); // vcon is connection with database
da.Fill(dt);
foreach (DataRow myRow in dt.Rows)
{
for (int x = 0; x {
string name = dt.Rows[x].ItemArray.GetValue(1).ToString();
if (name == textbox1.text) // here you can compare your access data with textbox
{
MessageBox.Show("Found");
}
else
{
MessageBox.Show("No");
}
}
}
}
希望这将是一个很棒的解决方案.谢谢大家.
Hopefully it would be really greateful solution. Thanks to all.
这篇关于如何使用Access数据库从C#中的datagridview打印选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文