ComboBox选择的值始终显示system.data.datarowview [英] ComboBox selected value always shows system.data.datarowview

查看:71
本文介绍了ComboBox选择的值始终显示system.data.datarowview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在我的窗口应用程序中使用了一个组合框。

组合框使用valuemember和displaymember与数据源绑定。

和我正在尝试填充我的datagridview的combobox的selectedindexchenged。

和im通过combobox.selectedvalue这样做。



但我总是得到一个错误,数据类型对布尔运算无效。

[数据类型(如果已知)= int,数据类型(如果已知)= nvarchar]



我的datagridview没有填写。



Hello,

I m using a combobox in my window's application.
And the combobox is bind with datasource using valuemember and displaymember.
and on selectedindexchenged of combobox i m trying to fill my datagridview.
and i m doing this through the combobox.selectedvalue.

But i always got an error that the datatype is not valid for boolean operation.
[Data type(if known)=int,data type(if known)=nvarchar]

and my datagridview does not fill.

private void Form_Load(object sender, EventArgs e)
        {
            try
            {
                label3.Visible = false;
                label4.Visible = false;
                string connstring = Properties.Settings.Default.TestDBConnectionString;
                con = new SqlCeConnection(connstring);
                con.Open();
                getallsubject();
                //getallunit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }










private void getallsubject()
        {
            try
            {
                da = new SqlCeDataAdapter("select SubjectId,SubjectName from Subject", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt != null)
                {
                    comboBoxsubject.DataSource = dt;
                    comboBoxsubject.ValueMember = dt.Columns[0].ToString();
                    comboBoxsubject.DisplayMember = dt.Columns[1].ToString();
                    //DataRow dr=dt.Rows[0];
                    //listBox1.DataSource = dt;
                    //listBox1.DisplayMember = dt.Columns[1].ToString();
                    //listBox1.ValueMember = dt.Columns[0].ToString();
                }
                else
                {
                    MessageBox.Show("No subject to add unit.Before proceed add subjects.");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }







private void comboBoxsubject_SelectedValueChanged(object sender, EventArgs e)
       {

              getallunit();

       }










private void getallunit()
       {
           try
           {
               if (comboBoxsubject.SelectedValue!="0")
               {
                   da = new SqlCeDataAdapter("select u.UnitId,u.UnitName,s.SubjectName from SubjectUnit u,Subject s where u.SubjectId=s.SubjectId and u.SubjectId='" +comboBoxsubject.SelectedValue + "'", con);
                   DataTable dt = new DataTable();
                   da.Fill(dt);
                   if (dt != null)
                   {
                       dataGridViewunit.DataSource = dt;
                   }
                   else
                   {
                       MessageBox.Show("No units has been added yet.");
                   }


               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

推荐答案

我建​​议你改变订单

I would suggest you to change the order
comboBoxsubject.ValueMember = dt.Columns[0].ToString();
comboBoxsubject.DisplayMember = dt.Columns[1].ToString();
comboBoxsubject.DataSource = dt;


我遇到了同样的问题并尝试了
I was having the same problem and tried
dt.Columns[0].ToString();

哪个工作正常!经过一番检查后,我得出结论,这个问题与区域设置有关,因为除了一个具有不同区域设置的客户端(在我的情况下是土耳其)之外,它可以在其他地方工作。

which worked fine! After some inspection I came to the conclusion that this problem has to do with regional settings, since it works everywhere else except one client that has different regional settings (Turkey in my case).


尝试这在你的Dataadapter中



Try this in your Dataadapter

da = new SqlCeDataAdapter("select u.UnitId,u.UnitName,s.SubjectName from SubjectUnit u,Subject s where u.SubjectId=s.SubjectId and s.SubjectId='" +comboBoxsubject.SelectedValue + "'", con)

OR

da = new SqlCeDataAdapter("select u.UnitId,u.UnitName,s.SubjectName from SubjectUnit u,Subject s where s.SubjectId='" +comboBoxsubject.SelectedValue + "'", con);

;


这篇关于ComboBox选择的值始终显示system.data.datarowview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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