日期:2014-05-19  浏览次数:20956 次

datagridview控件不显示
SqlConnection   thisConnection   =   new   SqlConnection(@ "Data   Source=XXZX_M;Initial   Catalog=zhou;Integrated   Security=SSPI ");
                        thisConnection.Open();
                        SqlCommand   thisCommand   =   thisConnection.CreateCommand();
                        thisCommand.CommandText   =   "Select   *   from   news ";
                        SqlDataReader   thisReader   =   thisCommand.ExecuteReader();
                        dataGridView1.DataSource   =   thisReader;
                       
                        thisReader.Close();
                        thisConnection.Close();


结果datagridview中什么都不显示,连列名都没有.
datagridview好像也没有databind方法呀.

------解决方案--------------------
sorry,
this.Connection = thisConnection; //这一行没必要,刚才没看清楚.

DataTable dt = new DataTable();

dt.Load(thisCommand.ExecuteReader(CommandBehavior.CloseConnection));

dataGridView1.DataSource = dt;

这三行也可以写成:

BindingSource bs = new BindingSource();
bs.Load(thisthisCommand.ExecuteReader(CommandBehavior.CloseConnection));
dataGridView1.DataSource = bs;



------解决方案--------------------
DataGridView的DataSource不能指定为DataReader的