读书人

datagridview控件不显示,该怎么解决

发布时间: 2012-02-26 20:19:45 作者: rapoo

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的

读书人网 >C#

热点推荐