怎么解决在WinForm下拉列表ComboBox显示System.Data.DataRowview的问题啊?无法加载到数据库的数据,整了好久,没能突破的啊!望高手给予指正!在线等待

解决方案 »

  1.   

    如果你显示的是System.Data.DataRowview
    证明你加载值的时候加载的是对象.你需要把对象转换为.ToString()来试试
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace HypCRM
    {
        public partial class frmLogin : Form
        {
            public static string H_str_name;//记录登录用户名字
            public static string H_str_pwd;//记录登录用户密码
            public static string H_str_right;//记录登录用户的权限
            HypCRM.BaseClass.BaseOperate boperate = new HypCRM.BaseClass.BaseOperate();
            HypCRM.BaseClass.OperateAndValidate opAndvalidate = new HypCRM.BaseClass.OperateAndValidate();        public frmLogin()
            {
                InitializeComponent();
            }        private void frmLogin_Load(object sender, EventArgs e)
            {
                opAndvalidate.cboxBind("select UserName from tb_User", "tb_User", "UserName", cboxUName);
            }        private void cboxUName_SelectedIndexChanged(object sender, EventArgs e)
            {
                SqlDataReader sqlread = boperate.getread("select UserName,UserRight from tb_User where UserName='"+cboxUName.Text+ "'");
                if (sqlread.Read())
                {
                    labURight.Text = sqlread["UserRight"].ToString();
                    H_str_right = labURight.Text;
     
                }
                sqlread.Close();
            }        private void btnLogin_Click(object sender, EventArgs e)
            {
                SqlDataReader sqlread = boperate.getread("select UserName,UserPwd from tb_user where UserName='"+cboxUName.Text.Trim()+"'and UserPwd='"+txtPwd.Text.Trim()+"'");
                sqlread.Read();
                if (sqlread.HasRows)
                {
                    H_str_name = cboxUName.Text;
                    H_str_pwd = txtPwd.Text.Trim();
                    frmMain fmain = new frmMain();
                    this.Hide();
                    fmain.Show();            }
                else
                {
                    MessageBox.Show("用户名或密码错误!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    txtPwd.Text = "";
                    cboxUName.Focus();
                }
                sqlread.Close();        }        private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.Exit();
            }      
        }
    }
      

  3.   

    还有一种可能性就是你在绑定后要指定以下两个属性的值this.ComboBox.ValueMember = "ID字段";
    this.ComboBox.DisplayMember = "显示字段";
      

  4.   

    opAndvalidate.cboxBind重点是要类的这个方法代码.贴一下.或者在你上面的代码下面加个
    this.cboxUName.ValueMember="ta_User";
    this.cboxUName.DisplayMember="UserName";
      

  5.   

    能不能给我说说为什么要加入:
    或者在你上面的代码下面加个 
    this.cboxUName.ValueMember="ta_User"; 
    this.cboxUName.DisplayMember="UserName";嘿嘿~问题解决了,相当激动!感谢,QQ号码多少呢?加个好友吧!
      

  6.   

    [email protected]用QQ查找我邮箱就行.
    结贴吧
      

  7.   

    因为你提取出来的对象.所以显示的所有项都是对象,就是你之前的DataRowview故应指定属性字段.C#会自动给你转换为要显示的字符串,也就是提取你所指定的字段值来显示
      

  8.   


    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection("strConnection"); 
        SqlDataAdapter sqlda = new SqlDataAdapter("select * from 数据表名称 where 条件 ", myConn);
        DataTable dt = new DataTable("tb_User"); 
        sqlda.Fill(dt);
        cboxUName.DataSource = dt;
        cboxUName.DisplayMember = "UserName";
        //cboxUName.ValueMember = "";           // 如果返回值存另一个表,加上这行. 登录验证,感觉没必要
    }