0

每次我的表单加载/打开时,它都会提示“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());
    }
4

1 回答 1

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 所指出的,只需在分配数据源之前设置DisplayMember和属性,您的原始代码就可以工作:ValueMember

cmbProcessorMethods.ValueMember = "method_id";
cmbProcessorMethods.DisplayMember = "method_name";
cmbProcessorMethods.DataSource = dt;
于 2012-11-27T23:19:39.613 回答