using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace GrenderCount
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();        SqlConnection cn = new SqlConnection("data source=.;database=Myschool;integrated security=true;");
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            IntiailGrender();
        }
        private void IntiailGrender()
        {
            string sql = string.Format("select * from Grade");
            SqlDataAdapter apter = new SqlDataAdapter(sql, cn);
            apter.Fill(ds, "gr");            DataRow dr;            dr = ds.Tables["gr"].NewRow();
            dr["GradeId"] = 0;
            dr["GradeName"] = "全部";            ds.Tables["gr"].Rows.InsertAt(dr, 0);            this.cboGrender.DataSource = ds.Tables["gr"];
            this.cboGrender.DisplayMember ="GradeName";
        }        private void Bname_Click(object sender, EventArgs e)
        {
            SearchStudent();
        }
        public void SearchStudent()
        {
            try 
            {
                StringBuilder sql = new StringBuilder("SELECT [StudentNo],");
                sql.AppendLine("[StudentName],[Gender],[Birthday]");
                sql.AppendLine("[from [Student]");
               if(Convert.ToInt32(this.cboGrender.SelectedValue) != -1) /*这句貌似错了吗? 为什么系统会显示这句话 无法将类型为 "System.Data.DataRowView" 的对象强制转换为类型为 "System.IConvertible" 这是为什么? */
               {
                    sql.AppendFormat(" where [GradeId]={0}",Convert.ToInt32(this.cboGrender.SelectedValue));                }
                sql.Append(" order by [StudentNo]");
                SqlDataAdapter adapterStudent = new SqlDataAdapter(sql.ToString(), cn);
                
                if (ds.Tables["Student"] != null)
                {
                    ds.Tables["Student"].Clear();
                }
                adapterStudent.Fill(ds, "Student");
            }                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

解决方案 »

  1.   

    因为System.Data.DataRowView没有继承 "System.IConvertible" 接口,
    而且也不可能拿DataRowView转换到Int32上。
    你至少要这么写Convert.ToInt32(((DataRowView)this.cboGrender.SelectedValue)[0]),意思是,取DataRowView中的某个列的值。
      

  2.   

    问题已经解决了
     this.cboGrender.DataSource = ds.Tables["gr"];
     this.cboGrender.DisplayMember ="GradeName";
    在这后面加上这句话就就OK了 。
     this.cboGender.ValueMember ="StudentId"; 
      

  3.   

    非常感谢 已经解决了
    问题已经解决了
     this.cboGrender.DataSource = ds.Tables["gr"];
     this.cboGrender.DisplayMember ="GradeName";
    在这后面加上这句话就就OK了 。
     this.cboGender.ValueMember ="StudentId";