我怎样才能获得列表框
问题描述:
string KaloriSorgusu = "use FoodDB select drink_kal from Drinks";
SqlConnection baglanti2 = new SqlConnection("Data Source=" + IPFORM.ip.ToString() + "\\SQLEXPRESS;Initial Catalog=UserDB;User Id=Levent; Password=21012101;Trusted_Connection=False;Integrated Security=False");
using (baglanti2)
{
using (SqlCommand KaloriKomutu = new SqlCommand(KaloriSorgusu, baglanti2))
{
baglanti2.Open();
using (SqlDataReader KALORİokuyucu = KaloriKomutu.ExecuteReader())
{
while (KALORİokuyucu.Read())
{
lb_Kalori.Items.Add(KALORİokuyucu.GetValue(0).ToString());
total = lb_Kalori.Items.Count;
}
}
}
}
的选择指标的值此代码填充列表框,当我试图从选定的指数值,我似乎无法找到一种方式来获得的指数值。我怎样才能获得列表框
答
可能是这个小片段将帮助你。
// Get the currently selected item in the ListBox.
string curItem = listBox1.SelectedItem.ToString();
// Find the string in ListBox2.
int index = listBox2.FindString(curItem);
// If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
if(index == -1)
MessageBox.Show("Item is not available in ListBox2");
else
listBox2.SetSelected(index,true);`
目前还不清楚你想干什么,你有'KALORİokuyucu'如果你想分配给'lblKalori'你应该做'lb_Kalori该值将从'SqlDataReader' Object'保存值现在是什么.Items.Add((string)KALORİokuyucu[“FieldNameYouWant”]);'你也确定只有'1 Item'在分配给标签时会被返回吗?否则如果返回多个记录,则将覆盖以前的分配。 '这意味着你需要将.ExcuteReader()调用改为ExecuteScalar()' – MethodMan 2013-04-08 19:01:12
为什么不把'baglanti2'的实例化放入'using'块? – 2013-04-08 19:03:11