簡體   English   中英

顯示數據網格視圖

[英]show data grid view

當我按下按鈕“agregar”時,我在顯示網格視圖的數據時遇到問題:

在此處輸入圖片說明

它顯示一個小方塊,不顯示任何數據。

cn.Open();
MySqlCommand cm = cn.CreateCommand();
cm.CommandType = CommandType.Text;
cm.CommandText = "select * from detalle where iddetalle= '" + txt_boleta.Text + "'and idlocal='" + txtlocal.Text + "'";
cm.ExecuteNonQuery();
MySqlDataAdapter da = new MySqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds,"data");
GridView1.DataSource = ds.Tables["data"];
GridView1.DataBind();

您有您的查詢的幾個問題,一個快速的解決方法是通過在之間給人一種空間"' ,並and"'and ,通過這個你是通過注射開門黑客,所以,更好的選擇是使用參數化查詢。 還有一些建議:

  1. 您正在使用適配器將查詢結果收集到 DataTable/DataSet,因此您無需在此之前執行查詢
  2. 您正在使用單個查詢獲取值,因此沒有必要在此處使用 DataSet,然后從 Dataset 中獲取所需的表,而不是您可以使用 Adapter 直接將結果表獲取到 DataTable。
  3. 您也可以使用 Using 塊

總之綁定網格的代碼應該是這樣的:

DataTable dsDetalle=new DataTable("Data");           
using (MySqlCommand commandSql = cn.CreateCommand())
{
    commandSql.CommandType = CommandType.Text;
    commandSql.CommandText = "select * from detalle where iddetalle=@iddetalle and idlocal=@idlocal";
    commandSql.Parameters.AddWithValue("@iddetalle", "txt_boleta.Text");
    commandSql.Parameters.AddWithValue("@idlocal", "txtlocal.Text");
    MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(commandSql);
    sqlAdapter.Fill(dsDetalle);
}
GridView1.DataSource = dsDetalle;
GridView1.DataBind();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM