天天看点

DataReader绑定DataGridView的两种方法

第一种:借助于BindingSource

sqlDataReader Sdr=通过查询函数得到的sqlDataReader类型的数据;

BindingSource Bs=new BindingSource() ;

Bs.DataSource=Sdr;

DataGridView.DataSource=Bs;

第二种:借助DataTable

sqlDataReader Sdr=通过查询函数得到的sqlDataReader类型的数据;

DataTable Dt=new DataTable();

Dt.Load(Sdr);

DataGridView.DataSource=Bs;

获取DataGridView的选择的行

例如将选择的行的第一列的值转换成string的类型并赋给Num

string Num=Convert.ToString(DataGridView[0,DataGridView.CurrentCell.RowIndex].Value);

这样选中的行的第一列就转换成了string类型的数据,可以通过SQL语句进行其他的操作。

更多.net内容请见:http://bbs.netluntan.com