Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以´;t将ToolStripComboBox SelectedValue转换为Int32 C#_C# - Fatal编程技术网

我可以´;t将ToolStripComboBox SelectedValue转换为Int32 C#

我可以´;t将ToolStripComboBox SelectedValue转换为Int32 C#,c#,C#,我将一个C#ToolStripComboBox绑定到一个数据表,当我更改选择(事件)时,我尝试获取SelectedValue,错误消息说属性是DataRowView,当我将其转换为DataRowView时,错误消息说我无法从Int32转换为DataRowView。我该怎么办?谢谢这对我来说很好 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using

我将一个C#ToolStripComboBox绑定到一个数据表,当我更改选择(事件)时,我尝试获取SelectedValue,错误消息说属性是DataRowView,当我将其转换为DataRowView时,错误消息说我无法从Int32转换为DataRowView。我该怎么办?谢谢这对我来说很好

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();

            DataColumn dc1 = new DataColumn("id", typeof(System.Int32));
            DataColumn dc2 = new DataColumn("desc", typeof(System.String));

            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["id"] = i;
                dr["desc"] = i.ToString() + " desc";

                dt.Rows.Add(dr);
            }


            toolStripComboBox1.ComboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;

            toolStripComboBox1.ComboBox.SelectionChangeCommitted += ComboBox_SelectionChangeCommitted;

            toolStripComboBox1.ComboBox.ValueMember = "id";
            toolStripComboBox1.ComboBox.DisplayMember = "desc";
            toolStripComboBox1.ComboBox.DataSource = dt;


        }

        private void ComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            int v = Int32.Parse(((DataRowView)toolStripComboBox1.ComboBox.SelectedItem)["id"].ToString());

            MessageBox.Show("User Selected " + v.ToString());
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            //this line will give you the 


        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
DataTable dt=新的DataTable();
DataColumn dc1=新的DataColumn(“id”,typeof(System.Int32));
DataColumn dc2=新的DataColumn(“desc”,typeof(System.String));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
对于(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr[“id”]=i;
dr[“desc”]=i.ToString()+“desc”;
dt.Rows.Add(dr);
}
toolStripComboBox1.ComboBox.SelectedIndexChanged+=ComboBox\u SelectedIndexChanged;
toolStripComboBox1.ComboBox.SelectionChangeCommitted+=ComboBox\u SelectionChangeCommitted;
ToolStripComboBox 1.ComboBox.ValueMember=“id”;
ToolStripComboBox 1.ComboBox.DisplayMember=“desc”;
toolStripComboBox1.ComboBox.DataSource=dt;
}
private void组合框\u SelectionChangeCommitted(对象发送方,事件参数e)
{
//抛出新的NotImplementedException();
int v=Int32.Parse(((DataRowView)toolStripComboBox1.ComboBox.SelectedItem)[“id”].ToString());
Show(“用户选择的”+v.ToString());
}
private void组合框\u SelectedIndexChanged(对象发送方,事件参数e)
{
//这条线会给你
}
私有void ToolStripCombox1_单击(对象发送方,事件参数e)
{
}
}
}

我需要将SelectedValue获取为Int32。您必须使用SelectedValue:Int32.Parse(((DataRowView)toolstrip.SelectedValue)[“ID”].ToString())这样做:Int32.Parse(((DataRowView)comboBox.SelectedValue)[“ID”]。ToString()抛出错误“无法从System.Int32类型转换为System.Data.DataRowView类型”不要使用相同的名称“Id”使用您要识别记录的名称。你能给我一个你试图转换的值的样本吗?在这种情况下,如果我用鼠标指向源代码中的SelectedValue,我可以看到数字