Combobox和System.Data.DataRowView [英] Combobox and System.Data.DataRowView

查看:108
本文介绍了Combobox和System.Data.DataRowView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,当主窗体打开时调用

I have a method that is called when the main form is opened

private void populateComboBoxes()
{
    SqlCeDataAdapter breakfastAdapter = new SqlCeDataAdapter("SELECT DISTINCT recipeName FROM Recipe WHERE Category = 'breakfast' ", databaseConnection);
    SqlCeDataAdapter lunchAdapter = new SqlCeDataAdapter("SELECT DISTINCT recipeName FROM Recipe WHERE Category = 'lunch' ", databaseConnection);
    breakfastAdapter.Fill(breakfastDS, "Recipe");
    lunchAdapter.Fill(lunchDS, "Recipe");
    cmbBox1.DisplayMember = "recipeName";
    cmbBox1.ValueMember = "recipeName";
    cmbBox1.DataSource = breakfastDS.Tables["Recipe"];
    cmbBox2.DataSource = lunchDS.Tables["Recipe"];            
    cmbBox2.DisplayMember = "recipeName";
    cmbBox2.ValueMember = "recipeName";                      
}

它基本上根据SELECT语句填充两个组合框。一旦此方法命中 cmbBox1.DataSource = breakfastDS.Tables [Recipe] ,它停止执行并移动到此方法:

which essentially populates the two comboboxes based on the SELECT statements. Once this method hits cmbBox1.DataSource = breakfastDS.Tables["Recipe"] it stops executing and moves to this method:

private void cmbBox1_SelectedIndexChanged(object sender, EventArgs e)
{         
    rtbPicture.Visible = false;
    string qry = "";
    qry = "SELECT DISTINCT ingredientName FROM Recipe WHERE recipeName = " + cmbBox1.SelectedItem.ToString();

    SqlCeCommand com = new SqlCeCommand(qry, databaseConnection);
    com.CommandText = qry;
    rtbRecipe.Text = com.ExecuteScalar().ToString();
}

这个方法应该执行select语句, code> richtextbox ,但由于某种原因 cmbBox1 未设置。从我的理解,第一个组合框已经设置了显示和值成员在上一个方法。然而,当我到达 cmbBox1.SelectedItem.ToString()它返回 System.Data.DataRowView 而不是组合框中的实际字符串。我不知道为什么它给我 System.Data.DataRowView 而不是字符串。

This method is supposed to execute the select statement and put that information in the richtextbox, but for some reason cmbBox1 isn't set. From my understanding the first combobox already had set the display and value members in the previous method. However, when I get to the cmbBox1.SelectedItem.ToString() it returns System.Data.DataRowView instead of the actual string in the combobox. I'm not sure why it's giving me System.Data.DataRowView instead of the string.

推荐答案

您正在使用 cmbBox1.SelectedItem.ToString()
您可能应该使用 SelectedValue

qry = "SELECT DISTINCT ingredientName FROM Recipe WHERE recipeName = " + cmbBox1.SelectedValue.ToString();

SelectedItem 获取实际 ComboBox item(在这种情况下为 DataRowView ),而 SelectedValue 您指定为 ComboBox ValueMember 的属性。

SelectedItem gets the actual ComboBox item (in this case a DataRowView), whereas SelectedValue gets the value of the property you specified as the ValueMember of the ComboBox.

这篇关于Combobox和System.Data.DataRowView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆