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.SelectedItem.Tostring() メソッドを使用して CboPorts ComboBox を解析しようとしているところです。Null do to DataView = {System.Data.DataRowView} で戻ってきます。

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

Guys のおかげで見つかりました。コードでこれを実行したい場合は、これがその方法です。

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 に答える