WinForm中DataGridView该如何绑定一个SqlDataReader(VS2005) RTRT,真是奇怪了2005中的居然很难看到DataBind()这个函数了,我以前一直用2003的,以前绑定一个SqlDataReader是绝对没问题的,现在居然不行了,DataGridView中居然找不到DataBind()了,晕 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 麻烦大家帮下忙好吗,难道没有人清楚WinForm中DataGridView怎么绑定数据的吗public partial class Form1 : Form{ public Form1() { InitializeComponent(); SqlParameter[] sqlParameters = new SqlParameter[2]; sqlParameters[0] = new SqlParameter("@SalesID", SqlDbType.VarChar, 13); sqlParameters[0].Value = "1102103202102"; sqlParameters[1] = new SqlParameter("@SubShopID", SqlDbType.VarChar, 6); sqlParameters[1].Value = "000001"; SqlDataReader dr = SQLHelper.ExecuteReader("server=localhost;user id=sa;password=111111;database=EBusiness", CommandType.StoredProcedure, "GetOneSales", sqlParameters); this.dataGridView1.DataSource = dr; //找不到this.dataGridView1.DataBind();网上搜过很多解答,都说的模模糊糊的,而且都是绑定到DataSet或DataTable,难道VS2005了就不能绑定到DataReader了 }} 不用DataBind,直接设置数据源就行了.. 我记得好像是不能直接绑定DataReader的..得用个BindingSource作个中介..SqlDataReader sdr=cmd.ExecuteReader();BindingSource bs=new BindingSource();bs.DataSource=sdr;this.DataGridView1.DataSource=bs; 测试例子如下: private void button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from studentDetails", con); SqlDataReader sdr = cmd.ExecuteReader(); BindingSource bs = new BindingSource(); bs.DataSource = sdr; this.dataGridView1.DataSource = bs; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } }测试通过>> winform数据绑定是WEB有点小小的区别,不用调用DataBind()方法,而且也不支持DataReader的绑定,你可以读取放到Dataset里面,再绑定this.dataGridView1.DataSource=你的datasetthis.dataGridView1.DataMameber=你的表名 鼠标滚动事件中Delta无法识别怎么回事 控件开发,错在怎么不出效果(代码如下) 关于基类与派生类的问题!没有分了, 等有分了一定加分, 谢谢 拼死都要弄懂,一个跟数据库有关的问题 csdn 目前无人能解决的问题 C#如何解决像网络邻居查找所有局域网客户端 我想大声疾呼!技术人员的才是最重要的!顺便散分! treeview添加或删除节点,如何能保存上? 三个双引号怎么处理? 急问:如何使用C#编程列出当前可用的SQL server服务器 请教大家关于byte类型数据的表示问题,谢谢! 请教c#中如何执行dos命令,如md等,又如何在c#中打开一个目录!!谢谢!!!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); SqlParameter[] sqlParameters = new SqlParameter[2];
sqlParameters[0] = new SqlParameter("@SalesID", SqlDbType.VarChar, 13);
sqlParameters[0].Value = "1102103202102";
sqlParameters[1] = new SqlParameter("@SubShopID", SqlDbType.VarChar, 6);
sqlParameters[1].Value = "000001";
SqlDataReader dr = SQLHelper.ExecuteReader("server=localhost;user id=sa;password=111111;database=EBusiness", CommandType.StoredProcedure, "GetOneSales", sqlParameters);
this.dataGridView1.DataSource = dr;
//找不到this.dataGridView1.DataBind();网上搜过很多解答,都说的模模糊糊的,而且都是绑定到DataSet或DataTable,难道VS2005了就不能绑定到DataReader了
}
}
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from studentDetails", con);
SqlDataReader sdr = cmd.ExecuteReader();
BindingSource bs = new BindingSource();
bs.DataSource = sdr;
this.dataGridView1.DataSource = bs;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}测试通过>>
而且也不支持DataReader的绑定,你可以读取放到Dataset里面,再绑定
this.dataGridView1.DataSource=你的dataset
this.dataGridView1.DataMameber=你的表名