0

在这里,我使用 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 对 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;
    }
}

我有一个奇怪的 Visual Studio Express 错误,我不确定发生了什么,但这应该可以正常工作,我不知道为什么,但我会在我的代码中解释。我不确定是否有另一种方法来处理这种方法。

4

2 回答 2

2

我找到了伙计们,谢谢,如果你想在你的代码中这样做,这就是如何做到的。

DataRowView drow = (DataRowView)cboPorts.SelectedItem;

            SelectedPort = drow.Row.ItemArray[0].ToString();
于 2013-07-24T00:12:48.653 回答
0

你应该申请支票

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

var row = (DataRowView)cboPorts.SelectedItem;  
string SelectedPort = row[0].ToString();
于 2013-07-23T17:47:08.343 回答