0

我有一个表ID和一个Name列,我想显示一个列IDName在一个DataGridView示例中,Textbox当我输入ID并单击搜索按钮时,datagridview 会显示特定的记录。在搜索Button中,我有以下代码,但它给出了错误

con.Open();
Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID = '"+textBox1.Text+"'")
sqldatareader reader = cmd.Executereader();
if (reader.HasRows)
{
          datagridview1.DataSource = reader.GetSqlValues()
 }

如何将数据绑定到 datagridview?

4

2 回答 2

1

代替数据阅读器使用dataSet

con.Open();

Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID = '"+textBox1.Text+"'");

sqlDataAdapter1 = new SqlDataAdapter();

sqlDataAdapter1.SelectCommand = cmd;

DataSet ds = new DataSet();

sqlDataAdapter1.Fill(ds);

datagridview1.DataSource = ds.table(0);

这将帮助你

于 2013-02-21T11:47:19.863 回答
0
con.Open();
Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID= '"+textBox1.Text+"'",con);
sqldatareader reader = cmd.Executereader();
if (reader.HasRows)
{
          datagridview1.DataSource = reader.GetSqlValues();
 }
于 2013-10-29T11:00:29.653 回答