组合框中选择的任何项目的选定值都给我null为什么 [英] selected value to any item selected in combobox give me null why

查看:61
本文介绍了组合框中选择的任何项目的选定值都给我null为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

Problem

组合框中任何项目的选定值都会为我提供空值原因

详细信息

我在Windows Form C#Visual Studio 2015中工作

I work in windows form c# visual studio 2015

而且我从Excel Sheet 2007中获得了组合框的数据

And i get data of combo box from excel sheet 2007

Excel工作表有两列

excel sheet have two column

MemberId代表组合框的值

MemberId represent value to combo box

MemberName代表文本到组合框

MemberName represent text to combo box

var fileName = string.Format("{0}\\Book3105", Directory.GetCurrentDirectory());  
        var connection = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};Mode=ReadWrite;Extended Properties=Excel 12.0 Xml;", fileName);  
  
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + "D:\\Book3105.xlsx" + ";" + "Extended Properties=\"Excel 12.0;HDR=YES\"");  
        OleDbConnection con = new OleDbConnection(connection);  
  
        try  
        {  
            con.Open();  
            str = "select * from [sheet2$]";  
            com = new OleDbCommand(str, con);  
            oledbda = new OleDbDataAdapter(com);  
            ds = new DataSet();  
            oledbda.Fill(ds, "[sheet2$]");  
            con.Close();  
  
            dt = ds.Tables["[sheet2$]"];  
            int i = 0;  
  
  
            for (i = 0; i <= dt.Rows.Count - 1; i++)  
            {  
                comboBox4.Items.Insert(0, "select member");  
                comboBox4.SelectedIndex = 0;  
                comboBox4.Items.Add(dt.Rows[i].ItemArray[1]);  
            }  
        }  
        catch (Exception ex)  
        {  
            MessageBox.Show(ex.Message);  
        }  



当调试按钮下的这一行时,请给我null

When debug this line under button give me null

当在组合框中选择任何项目时,消息框就会显示给我

and message box show to me when select any item in combo box 

private void buttontest_Click(object sender, EventArgs e)  
        {  
  
           if(Convert.ToInt32(comboBox4.SelectedValue)==0)  
            {  
                MessageBox.Show("wrong value");  
  
            }  
  
        }  



推荐答案

您可以执行以下操作

You could do something like this

.....
  dt = ds.Tables["[sheet2


"]; comboBox4.DisplayMember ="MemberName"; comboBox4.ValueMember ="MemberId"; comboBox4.DataSource = dt;
"]; comboBox4.DisplayMember = "MemberName"; comboBox4.ValueMember = "MemberId"; comboBox4.DataSource = dt;


这篇关于组合框中选择的任何项目的选定值都给我null为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆