想通过数据库往dataGridView里填充数据
大家好!这是我输入的代码,可是运行虽然没错误提示,但是结果dataGridView里也没填上数据,请问是哪里不对啊?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace lxy
{
public partial class Form2 : Form{
private SqlDataReader dr;
private DataSet ds;
private BindingSource bs;
public Form2()
{
InitializeComponent();
ds = new DataSet();
bs = new BindingSource();
string mycon = "Data Source=PC-20121027BVGK\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True";
SqlConnection Cn = new SqlConnection(mycon);
Cn.Open();
string ssql = "select * from student";
SqlDataAdapter ad = new SqlDataAdapter(ssql, Cn);
ad.Fill(ds, "student");
Cn.Close();
------解决方案--------------------没看到你给datagridview绑定数据集的方法。
加上:
dataGridView.DataSource=ds["student"];
------解决方案--------------------dataGridView.DataSource=ds["student"];
dataGridView.databind();
需要添加2个方法。。。