获取选定的数据库Combobox的值[英] Getting selected value of databound combobox

本文是小编为大家收集整理的关于获取选定的数据库Combobox的值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在这里,我使用dataAdpater和数据集表将数据加载到数据库中的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.secteditem.tostring()方法来解析cboports combobox的地方 - 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,我不确定发生了什么,但这应该在起作用,我不确定为什么,但是我会在代码中解释.我不确定是否有另一种处理此方法的方法.

推荐答案

我很感激,如果您想在代码中执行此操作,这就是如何做.

DataRowView drow = (DataRowView)cboPorts.SelectedItem;

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

其他推荐答案

您应该使用支票

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

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

本文地址:https://www.itbaoku.cn/post/1557168.html

问题描述

Here I am loading data to the CboPorts ComboBox from a database using dataAdpaters and Dataset table. This part works with no problems

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";
}

This is where I am trying to parse the CboPorts ComboBox, using the CboPorts.SelectedItem.Tostring() Method it keeps coming back with Null do to DataView = {System.Data.DataRowView} This keep coming in the Debugger Value 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;
    }
}

I am having a weird Visual Studio Express Bug, I am not sure what is going on but this is supposed to be working, I am not sure why but I will explain in my code. I am not sure if there is another way to handle this method.

推荐答案

I found Guys thanks, If you ever want to do this in your code this is how to do it.

DataRowView drow = (DataRowView)cboPorts.SelectedItem;

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

其他推荐答案

you should apply a check

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

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