c#
  • datagridview
  • sqlcommand
  • 2013-02-21 95 views 0 likes 
    0

    我有一個表IDName專欄中,我想顯示在DataGridView對於Textbox例的一個IDName當我進入ID並點擊搜索按鈕在DataGridView顯示specifc記錄。在搜索Button我有以下的代碼,但它給錯誤數據綁定到的DataGridView在C#

    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的?

    +5

    後錯誤請 – EProgrammerNotFound 2013-02-21 11:28:24

    +2

    什麼錯誤,如果要插入一個整數值來搜索和身份證也是數字,那麼爲什麼需要單引號?..請您的代碼更有幫助 – NetStarter 2013-02-21 11:31:03

    +1

    嘗試getValues(); – mcalex 2013-02-21 11:31:08

    回答

    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); 
    

    這將幫助你

    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(); 
    } 
    
    相關問題