comboBox1_SelectedIndexChanged事件问题

zjy30015563 2011-08-28 08:20:24
`private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=MyKTV;uid=sa;pwd=sa");
string strsql = string.Format("select * from singer_type");
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(strsql,conn);
adp.Fill(ds);
this.comboBox1.DataSource = ds.Tables[0];
this.comboBox1.DisplayMember = "singertype_name";
this.comboBox1.ValueMember = "singertype_id";
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
int id = Convert.ToInt16(this.comboBox1.SelectedValue);
}

每次一运行就会报错 无法将类型为“System.Data.DataRowView”的对象强制转换为类型“System.IConvertible”。

MessageBox.Show(this.comboBox1.SelectedValue.ToString()); 显示一开始两次SelectedValue的值是System.Data.DataRowView 不能转化为整数型,有什么解决的办法吗

...全文
791 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjy30015563 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zjy30015563 的回复:]
有谁能说说 为什么一开始运行SelectedValue的值是System.Data.DataRowView吗
[/Quote]

终于明白了,谢谢
lyl891201458 2011-08-28
  • 打赏
  • 举报
回复
string strsql = string.Format("select * from singer_type");------有问题!!!!!
改成: string strsql = string.Format("select singertype_name,singertype_id from singer_type");

huangwenquan123 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zjy30015563 的回复:]
有谁能说说 为什么一开始运行SelectedValue的值是System.Data.DataRowView吗
[/Quote]因为在datasource绑定的时候触发了SelectedIndexChanged,而此时还没有指定ValueMember,所以会弹出那个。
你把datasource放到最后面,运行的时候就不会弹出那个,而是弹出你绑定的第一个值。
zjy30015563 2011-08-28
  • 打赏
  • 举报
回复
有谁能说说 为什么一开始运行SelectedValue的值是System.Data.DataRowView吗
huangwenquan123 2011-08-28
  • 打赏
  • 举报
回复

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=MyKTV;uid=sa;pwd=sa");
string strsql = string.Format("select * from singer_type");
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(strsql,conn);
adp.Fill(ds);
this.comboBox1.DisplayMember = "singertype_name";
this.comboBox1.ValueMember = "singertype_id";
this.comboBox1.DataSource = ds.Tables[0];

}


  • 打赏
  • 举报
回复
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedItem.ToString());
}

zhlin118 2011-08-28
  • 打赏
  • 举报
回复
定义一个变量,判读是否首次加载。


private void Form1_Load(object sender, EventArgs e)
{

this.comboBox1.DataSource = GetDt();
this.comboBox1.DisplayMember = "id";
this.comboBox1.ValueMember = "name";

isFirst = false;
}
bool isFirst = true;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(!isFirst)
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}



zhlin118 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wnyxy 的回复:]

最简单的方法
C# code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
if(this.comboBox1.SelectedValue.ToString……
[/Quote]

正解,很少用微软自带的控件, 没想到这个控件还能支持disply和Value的形式。。。
zjy30015563 2011-08-28
  • 打赏
  • 举报
回复
if(this.comboBox1.SelectedValue.ToString()!=“System.Data.DataRowView”)
{
int id = Convert.ToInt16(this.comboBox1.SelectedValue);
}

这个是可以,但总觉得有其他解决方法吧
wnyxy001 2011-08-28
  • 打赏
  • 举报
回复
或者用个try catch 把异常吃掉
wnyxy001 2011-08-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wnyxy 的回复:]
最简单的方法

C# code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
if(this.comboBox1.SelectedValue.ToS……
[/Quote]

"System.Data.DataRow"
wnyxy001 2011-08-28
  • 打赏
  • 举报
回复
最简单的方法
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
if(this.comboBox1.SelectedValue.ToString()!=System.Data.DataRowView)
{
int id = Convert.ToInt16(this.comboBox1.SelectedValue);
}
}


zhlin118 2011-08-28
  • 打赏
  • 举报
回复
ToInt16 ?
private void Save2Excel() { //string file = "F:\\11\\ck.xlsx"; //string path = "F:\\11\\excel\\"; string time = DateTime.Now.ToString("yyyyMMdd"); string file = System.Windows.Forms.Application.StartupPath + "\\ck.xlsx"; string path = System.Windows.Forms.Application.StartupPath + "\\excel\\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string saveName1 = path + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"; excel = new CCExcel(file,saveName1); excel.Exceldata(); } private void textBox1_TextChanged(object sender, EventArgs e) { Form1.name = textBox1.Text.ToString(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.Text = 11.ToString(); comboBox2.Text = 100.ToString(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Form1.comB1 = comboBox1.Text.ToString(); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { Form1.comB2 = comboBox2.Text.ToString(); } } public class CCExcel { public Excel.Application appExcel; public Excel.Workbooks wbs; public Excel.Workbook wb; public Excel.Worksheets wss; public Excel.Worksheet ws; private string fileName; private string saveName; public CCExcel(string fileName,string saveName) { // //TODO: 在此处添加构造函数逻辑 // this.fileName = fileName; this.saveName = saveName; } public void Exceldata() { Create(); //Open(fileName); Data2Excel(); Save(saveName); //Save(wb,saveName); //appExcel.ActiveWorkbook.SaveCopyAs(fileName); //判断当前激活的表,并保存这个表。否则,保存时会弹出“是否保存Sheet1.xlsx”的对话框 wb.Close(Type.Missing, Type.Missing, Type.Missing); wbs.Close(); appExcel.Quit(); wb = null; wbs = null; appExcel = null; GC.Collect(); } private void Create()//创建一个Excel对象 { appExcel = new Excel.Application(); wbs = appExcel.Workbooks; wb = wbs.Add(true); //ws = (Excel.Worksheet)wb.ActiveSheet;//这是一个只读sheets集合 //Excel.Worksheet worksheet = wb.ActiveSheet as Excel.Worksheet;//这也是一个只读sheets集合 //Excel.Worksheet ws = (Worksheet)wb.Worksheets[1];//创建工作页sheet单页 ws = wb.Worksheets[1] as Worksheet; //第一个sheet页 ws.Name = "ck"; //这里修改sheet名称 } public void Open(string fileName) { appExcel = new Excel.Application(); wbs = appExcel.Workbooks; wb = wbs.Add(fileName); wb = wbs.Open(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); }
本系统开发的是一个比较完善宾馆管理系统,是为了对宾馆的住房和人员有一个较好的管理,为此开发了“宾馆管理系统”——基于管理系统。该系统适用于一些中小型宾馆的管理应用, 本系统采用用户管理,客房管理,房客预定等应用,给广大用户带来了很大的便利。本系统具有以下功能: 1)用户管理,该功能用于管理使用该系统的用户,以及按模块进行权限的分配。 2)房客管理,主要包括房客登记、补交押金、房客换房和房客退 3)预订管理,主要包括预定房间、预定转入住和类别。取消预定。 4)客房管理,主要包括客房信息、编辑客房和客房内的物品。 (自己完成) 开发工具:C#、SQL Server 2000。 private void loading1_Load(object sender, EventArgs e) { while (dr.Read()) { comboBox1.Items.Add(dr["userIdetity"].ToString()); } this.button1.Visible = false; this.button2.Visible = false; timer1.Interval = 1000; timer1.Start(); label4.Text = System.DateTime.Now.ToString(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Text == "前台服务员") { button1.Text = "登录前台系统"; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select username from userManagement where userIdetity=@userIdetity "; cmd.Parameters.Add(new SqlParameter("@userIdetity", comboBox1.SelectedItem.ToString())); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr["username"].ToString()); } dr.Close(); } else if (comboBox1.Text == "后台总经理") { button1.Text = "登录后台系统"; cmd.CommandText = "select userName from userManagement where userIdetity=@userIdetity "; cmd.Parameters.Add(new SqlParameter("@userIdetity", comboBox1.Text )); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr["userName"].ToString()); } }

110,539

社区成员

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

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

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