简体   繁体   English

C#ComboBox SelectedItem.toString()不返回预期结果

[英]C# ComboBox SelectedItem.toString() not returning expected results

I have a form that allows a user to add players to a roster, by entering the player name and selecting, from a combo box, the division to which the player belongs. 我有一个表格,允许用户通过输入玩家名称并从组合框中选择玩家所属的部门来将玩家添加到名册中。

When time comes to add the player to my TreeView control, the node that should display the division selected displays this text instead: System.Data.DataRowView 当需要将播放器添加到我的TreeView控件时,应该显示所选分区的节点将显示以下文本:System.Data.DataRowView

I got the code to implement this through MSDN here: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx 我在此处通过MSDN实现了此代码: https//msdn.microsoft.com/zh-cn/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx

Here's the code in the load function of the form, to fill the combo box: 这是表单的加载功能中的代码,用于填充组合框:

        private void frm_add_players_Load(object sender, EventArgs e)
    {
        Divisions divs = new Divisions();
        Players players = new Players();
        DataTable dtDivisions = divs.GetActiveDivisions(); //divisions combo box
        DataTable dtPlayers = players.GetPlayersByTourID(this.tourID);
        //set the forms datatable
        this.dt_players = dtPlayers;

        //fill the combo box
        this.cmbo_divisions.DataSource = dtDivisions;
        this.cmbo_divisions.DisplayMember = "title";
        this.cmbo_divisions.ValueMember = "ID";
        this.cmbo_divisions.SelectedIndex = -1;
        this.cmbo_divisions.Text = "Select a Division";

        //set treeview imagelist
        this.tview_roster.ImageList = tview_imagelist;
        this.tview_roster.ImageIndex = 1; //division icon

        //fill treeview
        foreach (DataRow dr in dtPlayers.Rows)
        {
            FillPlayerTreeview(dr);
        }

        //expand treeview
        this.tview_roster.ExpandAll();
        this.ActiveControl = this.txt_player_name;
    }

Here I call the function to add the player to the TreeView: 在这里,我调用将播放器添加到TreeView的函数:

    private void btn_add_Click(object sender, EventArgs e)
    {
        object selItem = cmbo_divisions.SelectedItem;
        AddPlayerToTreeView(txt_player_name.Text, selItem.ToString());
    }

And here is the function that adds the player: 这是添加播放器的功能:

        private void AddPlayerToTreeView(string playerName, string division)
    {
        TreeNode[] tns = this.tview_roster.Nodes.Find(division, false); //try to find the division, if exists
        TreeNode tn = new TreeNode();

        if (tns.Length > 0) //division exists - add player
        {
            tn = this.tview_roster.Nodes[tns[0].Index].Nodes.Add(playerName, playerName);
            tn.ImageIndex = 0; //player icon
        }
        else //division doesn't exist - add division, then add player
        {
            tn = this.tview_roster.Nodes.Add(division, division);
            tn.ImageIndex = 1; //division icon
            AddPlayerToTreeView(playerName, division);
        }
    }

And the result is this: 结果是这样的: cmbobox问题

I'm not sure why it won't work.. and I'm at a loss. 我不确定为什么它不起作用..我很茫然。 Any help would be appreciated. 任何帮助,将不胜感激。

Well, well... maybe something like the following. 好吧,好吧……也许像下面这样。

Access the combo's data source, which is a DataTable, and extract selected row and column value using selected index. 访问组合的数据源(即DataTable),并使用选定的索引提取选定的行和列值。 Maybe add some error handling, too. 也许也添加一些错误处理。

private void btn_add_Click(object sender, EventArgs e)
{
    var data = cmbo_divisions.DataSource as DataTable;
    var row = data.Rows[cmbo_divisions.SelectedIndex];
    var selected = row["title"].ToString();
    AddPlayerToTreeView(txt_player_name.Text, selected);
}

Try with: 尝试:

private void btn_add_Click(object sender, EventArgs e)
{
    AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.Items[cmbo_divisions.SelectedIndex].Text);
}

EDIT: Updated to a better way 编辑:更新为更好的方法

Try this : 尝试这个 :

private void btn_add_Click(object sender, EventArgs e)
{
    object selItem = cmbo_divisions.SelectedItem;
    AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.SelectedItem as string);
}

ToString() will get the type name, but in that case the SelectedItem is a string. ToString()将获得类型名称,但在这种情况下,SelectedItem是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM