初学者:查询语句报错!请大家帮忙改错!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace ConnectAccess
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private void button8_Click(object sender, System.EventArgs e)
{
string source="server=localhost;uid=sa;pwd=;database=student";
string select="select * from score";
SqlConnection conn=new SqlConnection(source);
conn.Open();
SqlCommand cmd =new SqlCommand(select,conn);
SqlDataReader reader=cmd.ExecuteReader();
this.dataGrid1.DataSource = reader;
}
}
}
我在datagrid中显示,但是报错,错误信息如下:
编译通过了,运行时出对话框
未处理的“System.Exception”类型的异常出现在 system.windows.forms.dll 中。
其他信息: 复杂的 DataBinding 接受 IList 或 IListSource 作为数据源
请大家帮忙!
问题点数:20、回复次数:8Top
1 楼jxufewbt(我的目标是5星)回复于 2005-10-30 14:51:12 得分 5
在this.dataGrid1.DataSource = reader;后面加上
this.dataGrid1.DataBind();
看看Top
2 楼herohero55(英雄)回复于 2005-10-30 15:01:24 得分 0
“System.Windows.Forms.DataGrid”并不包含对“DataBind”的定义
加上就出这个错误了。Top
3 楼zhy0101(莠)回复于 2005-10-30 15:09:14 得分 0
WinForm当中下列数据源有效:
DataTable
DataView
DataSet
DataViewManager
一维数组
任何实现 IListSource 接口的组件
任何实现 IList 接口的组件
不包括DataReader,使用DataSet
Top
4 楼herohero55(英雄)回复于 2005-10-30 15:36:28 得分 0
应该怎么写呢?
我是初学,不是很会Top
5 楼xlfeiyu()回复于 2005-10-30 16:09:58 得分 15
private void button8_Click(object sender, System.EventArgs e)
{
string source="server=localhost;uid=sa;pwd=;database=student";
string select="select * from score";
SqlConnection conn=new SqlConnection(source);
SqlDataAdapter da = new SqlDataAdapter(select, conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource = ds.Table[0];
}
}
}
Top
6 楼herohero55(英雄)回复于 2005-10-30 16:58:20 得分 0
并不包含对Table的定义。
报此错Top
7 楼xlfeiyu()回复于 2005-10-30 17:11:10 得分 0
this.dataGrid1.DataSource = ds;Top
8 楼herohero55(英雄)回复于 2005-10-30 17:17:25 得分 0
OK,谢谢大有,结帖!Top




