[英]How to get values of selected Listbox item into Listview
我正在開發C#Windows窗體應用程序。
現在,我試圖從列表框中選擇值,並使用存儲過程在列表視圖中加載相應的值。
這是我的存儲過程:
有人可以幫忙嗎?
謝謝
好的,如果我正確地解釋了您的問題,您正在詢問如何使用C#調用存儲過程?
這很容易做到,首先您需要初始化SqlConnection ,然后需要發出SqlCommand-將它們放在一起就可以對SQL數據庫進行調用。
這是有關如何進行簡單的存儲過程調用的示例。
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
你可以在這里讀更多關於它的內容。 如何執行存儲過程(MSDN)
您必須在列表框的選擇更改時編寫一個事件。然后您可以獲取所選值的值。
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox l = sender as ListBox;
var selectedvalue = l.SelectedItem.ToString();
}
那么您必須將訂單ID傳遞給SP,SP將返回該訂單ID的數據並將該數據綁定到ListView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.