日期:2014-05-18  浏览次数:20756 次

winfrom绑定dataGridView控件
第一次查询数据库所有信息,在窗体load进行绑定的时候没有问题都有值,但是,按上面的条件再进行查询的时候,dataGridView就没有值出现 ,但在后台执行SQL语句时有数据,打断点跟踪DAL方法时,同样每个属性都有值,但是就是在传回来的时候不好用,一片空白,附上代码
//窗体代码
C# code
 private void 场内车辆_Load(object sender, EventArgs e)
        {
           
            Bind();
        }


        public void Bind()
        {
            dgvRuChang.AutoGenerateColumns = false;
            dgvRuChang.DataSource = bsb.GetNoExitCar();
        }




             
        //根据车主姓名查询
        private void button1_Click(object sender, EventArgs e)
        {
            dgvRuChang.DataSource = null;
            dd();
           
        }

        public  void dd()
        {
             string param = "";
            switch (cbbTiaoJian.Text)
            {
                case "卡号":
                    param = txtParam.Text;
                    dgvRuChang.DataSource = bsb.GetByUserName(param); 
                    break;
                case "车主姓名":
                    param = txtParam.Text;
                    dgvRuChang.DataSource = bsb.GetByUserName(param); 
                    break;
                default:
                     param = txtParam.Text;
                    dgvRuChang.DataSource = bsb.GetByUserName(param);
                    break;
            }
        }

//后台DAL方法
C# code
 //场内车辆页面,根据车主名字查询
        public BusResordModel GetByUserName(string userName)
        {
            try
            {
                SqlParameter[] parm = { sqlH.MakeInParam("@userName", SqlDbType.NVarChar, 50, userName)};
                using (SqlDataReader dr = sqlH.ExecuteReader("SelectByUserName", CommandType.StoredProcedure, parm))
                {
                    BusResordModel busM = new BusResordModel();
                    while (dr.Read())
                    {
                        busM.CardNo = dr["cardNo"].ToString();
                        busM.UserName = dr["userName"].ToString();
                        busM.BusNo = dr["busNo"].ToString();
                        busM.BusType = dr["busType"].ToString();
                        busM.InTime = dr["inTime"].ToString();
                        busM.InAddr = dr["inAddr"].ToString();
                        busM.Picture = dr["picture"].ToString();
                        busM.Guid = dr["guid"].ToString();
                    }
                    return busM;
                }
            }
            catch
            {
                return null;
            }
        }

busM里的各个字段在打断点跟踪的时候均有值

------解决方案--------------------
返回一个类给dataGridView 当然没有显示了 返回别的吧