選擇命令基本上我想要的是數據網格視圖的填充這樣的查詢的DataGridView與參數
SELECT DealerName,的OrderId,DealerId,訂購日期,ItemType的,價格,數量,總,TotalBill FROM DBO。 DetailedRecord其中DealerName = ComboboxName.SelectedValue
我看不出如何添加參數,它,我不想通過工具條使用填充
感謝
選擇命令基本上我想要的是數據網格視圖的填充這樣的查詢的DataGridView與參數
SELECT DealerName,的OrderId,DealerId,訂購日期,ItemType的,價格,數量,總,TotalBill FROM DBO。 DetailedRecord其中DealerName = ComboboxName.SelectedValue
我看不出如何添加參數,它,我不想通過工具條使用填充
感謝
比方說,你想通過一些combobox.selectedvalue對數據進行篩選,你有一個提交按鈕,在提交按鈕的代碼,你初始化的一個新的DataTable類型yourdatasource.table像
YourDataSource.YourTableDataTable anything= new YourDataSource.YourTableDataTable();
yourdataadapter.fill(anything,parametervalue.tostring());
DataGridView1.datasource= anything;
而你一切都設定好了。
爲什麼不使用存儲過程,然後給它一個數據集來填充您的信息?
謝謝我找到了解決方法 – 2013-03-21 22:03:53
嘗試表綁定到您的DataGridView
。
請參閱以下一個簡單的例子:
MySqlConnection conn = new MySqlConnection(connectionstring);
conn.Open();
string stmt = "SELECT DealerName, OrderId, DealerId, OrderDate, ItemType, Price, Quantity,
Total, TotalBill FROM dbo.DetailedRecord where DealerName=ComboboxName.SelectedValue";
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(stmt, conn);
da.Fill(ds, "dbo.DetailedRecord");
dataGridView1.DataSource = ds.Tables["dbo.DetailedRecord"];
conn.Close();
只需將您的參數作爲字符串傳遞到您的查詢中即可。使用簡單的字符串連接(++)。注意如何仔細創建搜索字符串。確保您初始化數據表第一否則就會彈出關於空參數的錯誤:例如(可在SQL服務器,MySQL和Postgres)
String connectionString = "server = ...; db= ...; passwd = ....;";
DataTable dt_reservation_product_mix = new DataTable();
MySqlDataAdapter ad3;
ad3 = new MySqlDataAdapter("select `product`.`name`, `product`.`notes` from `product` where `product`.`code` = " + Convert.ToString(ComboboxName.SelectedValue) + "; ", connectionString);
ad3.Fill(dt_reservation_product_mix);
ad3.Dispose();
要與此查詢的結果來填充你的dataGridView
? – 2013-03-21 21:46:25