如何在变量中存储combox所选项 [英] How to store combox selected item in variable

查看:95
本文介绍了如何在变量中存储combox所选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我试图在变量中存储组合框所选项目的值。

但它无法正常工作。



根据我的代码,当我按下编辑按钮时,它会填充组合框。



当索引值改变时,它应显示我在组合框中选择的字符串。



但它的显示类似于

system.data.datarowview



什么我试过了:



private void btnedit_Click(object sender,EventArgs e)

{



cmdpr.Enabled = true;

SqlConnection connection = new SqlConnection(cn);

SqlCommand cmd1 = new SqlCommand(select ProjectName,来自Project,connection的ProjectID;

SqlDataAdapter da = new SqlDataAdapter(cmd1);

connection.Open();

DataSet ds = new DataSet();

da.Fill(ds,PN);

cmdpr.DisplayMember =ProjectName;

cmdpr.ValueMember =ProjectID;

cmdpr.DataSource = ds.Tables [PN];

connection.Close();

}



private void cmdpr_SelectedIndexChanged(object sender,EventArgs e)< br $>
{

string s = cmdpr.SelectedItem.ToString();

MessageBox.Show(s);



}

解决方案

尝试将cmdpr.SelectedItem强制转换为DataRowView对象,并按名称选择要使用的列,如下所示:



 MessageBox.Show((cmdpr.SelectedItem  as  DataRowView) .Row [  ProjectName]。ToString())


hi I am trying to store value of combo box selected Item in variable.
but it doesn't work properly.

according to my code when I press edit button it fills combo box.

and when index value changes it should show the string which I selected in combobox.

but its shows something like
system.data.datarowview

What I have tried:

private void btnedit_Click(object sender, EventArgs e)
{

cmdpr.Enabled = true;
SqlConnection connection = new SqlConnection(cn);
SqlCommand cmd1 = new SqlCommand("select ProjectName,ProjectID from Project", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
connection.Open();
DataSet ds = new DataSet();
da.Fill(ds, "PN");
cmdpr.DisplayMember = "ProjectName";
cmdpr.ValueMember = "ProjectID";
cmdpr.DataSource = ds.Tables["PN"];
connection.Close();
}

private void cmdpr_SelectedIndexChanged(object sender, EventArgs e)
{
string s = cmdpr.SelectedItem.ToString();
MessageBox.Show(s);

}

解决方案

Try casting the cmdpr.SelectedItem to a DataRowView object and select the column you want to work with by name like the following:

MessageBox.Show((cmdpr.SelectedItem as DataRowView).Row["ProjectName"].ToString())


这篇关于如何在变量中存储combox所选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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