C# 获取数据绑定组合框的选定值

C# 获取数据绑定组合框的选定值,c#,xml,linq,C#,Xml,Linq,在这里,我使用dataAdpaters和Dataset表从数据库将数据加载到CboPorts组合框。这部分工作没有问题 public void LoadPorts(string portpath) { //Loads Existing ClientGroups from specified tables string tablename = portpath; SqlConnection sqlConnectionCmdString = new SqlConnectio

在这里,我使用dataAdpaters和Dataset表从数据库将数据加载到CboPorts组合框。这部分工作没有问题

public void LoadPorts(string portpath)
{
    //Loads Existing ClientGroups from specified tables

    string tablename = portpath;
    SqlConnection sqlConnectionCmdString = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Rick\Documents\Visual Studio 2010\Projects\Server\database\ClientRegit.mdf;Integrated Security=True;User Instance=True");

    //Properly Defines the string for naming the table according to the systems naming scheme
    string Command = "SELECT Port FROM [" + tablename + "]";

    SqlCommand sqlCommand = new SqlCommand(Command, sqlConnectionCmdString);

    SqlDataAdapter objDA = new SqlDataAdapter(sqlCommand);

    DataSet dsGroups = new DataSet();

    objDA.Fill(dsGroups, "dtGroup");

    cboPorts.DataSource = dsGroups.Tables["dtGroup"];
    cboPorts.DisplayMember = "Port";
    cboPorts.ValueMember = "Port";
}
这就是我试图解析CboPorts组合框的地方,使用CboPorts.SelectedItem.Tostring()方法,它不断返回Null do to DataView={System.Data.DataRowView}这在调试器值Intel Sense中不断出现

private void cboPorts_SelectedIndexChanged(object sender, EventArgs e)
{ 
    string xmlpath = @"C:\[...]" + cboNetGuid.SelectedItem.ToString() + ".xml";

    XElement main = XElement.Load(xmlpath);

    //This is where it's supposed to parse the Selected Item of the combo box
    string SelectedPort = cboPorts.SelectedItem.ToString();

    //Linq query for searching IP address by ID Attributes
    IEnumerable<XElement> searched =
        from ip in main.XPathSelectElements("Row/ip_addresses")
        where (string)ip.Attribute("id") == SelectedPort //<--Passes the Value Here
        select ip;

    //Get the Ip from the selected port number
    foreach (string ips in searched)
    {
        Network_IP = ips.ToString();
    }

    //Linq Query to assign attributes to xml server data file
    IEnumerable<XElement> SearchedAttr =
        from proto in main.XPathSelectElements("Row/protocols")
        where (string)proto.Attribute("id") == SelectedPort
        select proto;

    foreach (string protos in SearchedAttr)
    {
        lstproto = protos.ToString();
    }

    //Linq query for searching security requests
    IEnumerable<XElement> SearchedSec =
        from Secure in main.XPathSelectElements("Row/security")
        where (string)Secure.Attribute("id") == SelectedPort
        select Secure;

    foreach (string Secur in SearchedSec)
    {
        Security = Secur.ToString();
    }
    //Linq query for searching created dates
    IEnumerable<XElement> SearchedCret =
        from created in main.XPathSelectElements("Row/creation_date ")
        where (string)created.Attribute("id") == SelectedPort
        select created;

    foreach (string Cret in SearchedCret)
    {
        Created = Cret.ToString();
    }
    //Define Channeling Form for Log Synchronizations
    //Adds the data to the form
    cboIP.Items.Add(Network_IP);
    cboIP.SelectedIndex = 0;
    cboProtocols.Items.Add(lstproto);
    cboProtocols.SelectedIndex = 0;
    if (Security == "YES")
    {
        cboEncrypt.SelectedIndex = 0;
    }
    else if (Security == "NO")
    {
        cboEncrypt.SelectedIndex = 1;
    }
}
private void cboPorts\u SelectedIndexChanged(对象发送方,事件参数e)
{ 
字符串xmlpath=@“C:\[…]”+cboNetGuid.SelectedItem.ToString()+“.xml”;
XElement main=XElement.Load(xmlpath);
//这是它应该解析组合框的选定项的地方
字符串SelectedPort=cboPorts.SelectedItem.ToString();
//用于按ID属性搜索IP地址的Linq查询
IEnumerable搜索=
从main.XPathSelectElements中的ip(“行/ip\U地址”)

其中(string)ip.Attribute(“id”)==SelectedPort/您应该应用一个检查

//check if user has selected anything 
if(cboPorts.SelectedIndex < 0)
 return;

var row = (DataRowView)cboPorts.SelectedItem;  
string SelectedPort = row[0].ToString();
//检查用户是否选择了任何内容
如果(cboPorts.SelectedIndex<0)
返回;
var row=(DataRowView)cboPorts.SelectedItem;
字符串SelectedPort=行[0]。ToString();

我找到了伙计们,谢谢,如果你想在你的代码中实现这一点,这就是方法

DataRowView drow = (DataRowView)cboPorts.SelectedItem;

            SelectedPort = drow.Row.ItemArray[0].ToString();

是否选择了任何组合框项?是的,它会在组合框中加载值,我应该能够选择其中一个值。我不确定如何使用GetItemText方法获取数据绑定表,它类似于以下cboPorts.GetItemText(MYDATAITEM);不,两者都不起作用我需要获取selecteditem值,因为有多个值加载到组合框中,问题是它没有解析它。我不确定问题是否可能,因为我正在使用数据绑定或其他让我困惑的东西,导致此过程起作用,当我改变将数据加载到combo它不起作用如果我在组合框中使用一个数据源,我想知道的是,我是否仍然可以使用cboPorts.SelectedItem.Tostring()呢方法从组合框中获取选定值。在调试器上,我获取的是该值,而不是选定值System.Data。DataRowView@shawn我想你自己也找到了答案,但我也更新了我的答案。