使用C#中的组合框加载表单中的“System.Data.DataRowView”

时间:2012-11-27 23:13:58

标签: c# winforms

每次我的表单加载/打开时都会提示“System.Data.DataRowView”,我怎么可能删除它呢?

这是我的代码:

    {
        InitializeComponent();
        GetProcessorCardTypes();
    }

    private void GetProcessorCardTypes()
    {
        cn.Open();

        MySqlCommand cmd = new MySqlCommand("call GetProcessorMethod(1)", cn);
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        cmbProcessorMethods.DataSource = dt;
        cmbProcessorMethods.ValueMember = "method_id";
        cmbProcessorMethods.DisplayMember = "method_name";
    }

    private void cmbProcessorMethods_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show(cmbProcessorMethods.SelectedValue.ToString());
    }

1 个答案:

答案 0 :(得分:1)

如果要完全删除消息框,请删除cmbProcessorMethods_SelectedIndexChanged事件处理程序。或者您可以将其更改为显示所选行的method_name

private void cmbProcessorMethods_SelectedIndexChanged(object sender, EventArgs e)
{
     DataRowView row = (DataRowView)cmbProcessorMethods.SelectedItem;
     MessageBox.Show(row["method_name"].ToString());
}

正如Derek指出的那样,只需在分配数据源之前设置DisplayMemberValueMember属性,原始代码就可以运行:

cmbProcessorMethods.ValueMember = "method_id";
cmbProcessorMethods.DisplayMember = "method_name";
cmbProcessorMethods.DataSource = dt;