DataGridView绑定List与绑定DataTable的区别

            //直接绑定DataTable
            string pnsql = @"select pId,pTypeId,pName,pCellPhone,pHomePhone,phoneType.ptName from  phoneNum,phoneType where PhoneNum.pTypeId=PhoneType.ptId";
            DataTable dtpn = SqlHelper.ExecuteTable(pnsql);
            this.dgvPerson.DataSource = dtpn;






            //使用的时候返回的是一个object类型,需要进行装箱拆箱
            object i = this.dgvPerson.CurrentRow.Cells[1].Value;  //装箱
            string j=i.ToString();   //拆箱
            MessageBox.Show(j);
            //获得DataTable后转化为List<T>再进行绑定
            string pnsql = @"select pId,pTypeId,pName,pCellPhone,pHomePhone,phoneType.ptName from  phoneNum,phoneType where PhoneNum.pTypeId=PhoneType.ptId";
            DataTable dtpn = SqlHelper.ExecuteTable(pnsql);
            List<PhoneNum> pnlist = SqlHelper.GetPNList(dtpn);    //对获得的DataTable转换成List<T>
            this.dgvPerson.DataSource = pnlist;     //绑定List<T>





            PhoneNum pntemp = this.dgvPerson.CurrentRow.DataBoundItem as PhoneNum;   //将获得的绑定想强转为实体类对象,然后可以直接调用
            txtName.Text = pntemp.PName.ToString();
            txtPhone.Text = pntemp.PHomePhone.ToString();
            txtCellPhone.Text = pntemp.PCellPhone.ToString();
            cboPtName.Text = pntemp.PtName.ToString();

 

DataGridView绑定List<T>与绑定DataTable的区别

上一篇:022 程序的控制结构


下一篇:WebAPI POST GET