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

时间:2013-07-23 17:38:19

标签: c# xml linq

这里我使用dataAdpaters和Dataset表从数据库将数据加载到CboPorts ComboBox。这部分没有问题

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 ComboBox的地方,使用CboPorts.SelectedItem.Tostring()方法,它继续使用Null do返回DataView = {System.Data.DataRowView}这继续调试器值-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;
    }
}

我有一个奇怪的Visual Studio Express Bug,我不知道发生了什么,但这应该是有效的,我不知道为什么,但我会在我的代码中解释。我不确定是否有其他方法来处理这种方法。

2 个答案:

答案 0 :(得分:2)

我发现伙计们,谢谢,如果您想在代码中执行此操作,那么这就是该怎么做。

DataRowView drow = (DataRowView)cboPorts.SelectedItem;

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

答案 1 :(得分:0)

你应该申请支票

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

var row = (DataRowView)cboPorts.SelectedItem;  
string SelectedPort = row[0].ToString();