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 作为数据源
请大家帮忙!

解决方案 »

  1.   

    在this.dataGrid1.DataSource = reader;后面加上
    this.dataGrid1.DataBind();
    看看
      

  2.   

    “System.Windows.Forms.DataGrid”并不包含对“DataBind”的定义加上就出这个错误了。
      

  3.   

    WinForm当中下列数据源有效: DataTable 
    DataView 
    DataSet 
    DataViewManager 
    一维数组 
    任何实现 IListSource 接口的组件 
    任何实现 IList 接口的组件 不包括DataReader,使用DataSet
      

  4.   

    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];
    }     }
    }    
      

  5.   

    并不包含对Table的定义。
    报此错
      

  6.   

    this.dataGrid1.DataSource = ds;