怎么解决在WinForm下拉列表ComboBox显示System.Data.DataRowview的问题啊?

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

在线等待。。。
...全文
708 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzsh0622 2009-09-17
  • 打赏
  • 举报
回复

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 = ""; // 如果返回值存另一个表,加上这行. 登录验证,感觉没必要
}

风之影子 2009-09-17
  • 打赏
  • 举报
回复
因为你提取出来的对象.

所以显示的所有项都是对象,就是你之前的DataRowview

故应指定属性字段.C#会自动给你转换为要显示的字符串,也就是提取你所指定的字段值来显示
风之影子 2009-09-17
  • 打赏
  • 举报
回复
libinguest@163.com用QQ查找我邮箱就行.


结贴吧


applegeren 2009-09-17
  • 打赏
  • 举报
回复
能不能给我说说为什么要加入:
或者在你上面的代码下面加个
this.cboxUName.ValueMember="ta_User";
this.cboxUName.DisplayMember="UserName";

嘿嘿~问题解决了,相当激动!感谢,QQ号码多少呢?加个好友吧!
风之影子 2009-09-17
  • 打赏
  • 举报
回复
opAndvalidate.cboxBind

重点是要类的这个方法代码.

贴一下.



或者在你上面的代码下面加个
this.cboxUName.ValueMember="ta_User";
this.cboxUName.DisplayMember="UserName";
风之影子 2009-09-17
  • 打赏
  • 举报
回复
还有一种可能性就是你在绑定后要指定以下两个属性的值

this.ComboBox.ValueMember = "ID字段";
this.ComboBox.DisplayMember = "显示字段";
applegeren 2009-09-17
  • 打赏
  • 举报
回复
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();
}


}
}
风之影子 2009-09-17
  • 打赏
  • 举报
回复
如果你显示的是System.Data.DataRowview
证明你加载值的时候加载的是对象.

你需要把对象转换为.ToString()来试试
风之影子 2009-09-17
  • 打赏
  • 举报
回复
贴出代码.

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧