关于DataGridView在WinForm的基础应用问题,请大家帮帮我吧!

fino_wang 2008-01-07 10:31:14
刚学习DataGridView,想编一个win程序,功能很简单,就是一个DataGridView控件用于显示数据库中一个表的所有内容,但是我写的这个程序虽然语法没有问题,但是确不能显示,请大家帮帮我,谢谢了!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//1---建立连接
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=SJ_SHIPPING_DB_NEW");

//2---创建Adapter
SqlDataAdapter Adapter = new SqlDataAdapter(@"SELECT * FROM dbo.DECLARE_I_INFO_REL",connection);

//3---建立一个数据集
DataSet ds = new DataSet();

//4---将Adapter的内容填充至数据集
Adapter.Fill(ds);

this.dataGridView1.DataSource = ds;
}
}
}
...全文
112 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyondzxn 2008-01-07
  • 打赏
  • 举报
回复
那就说明dataGridView1_CellContentClick没有被执行
fino_wang 2008-01-07
  • 打赏
  • 举报
回复
代码如下就好了


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//1---建立连接
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=SJ_SHIPPING_DB_NEW");

//2---创建Adapter
SqlDataAdapter Adapter = new SqlDataAdapter(@"SELECT * FROM dbo.DECLARE_I_INFO_REL", connection);

//3---建立一个数据集
DataSet ds = new DataSet();

//4---将Adapter的内容填充至数据集
Adapter.Fill(ds);

this.dataGridView1.DataSource = ds.Tables[0];
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
fino_wang 2008-01-07
  • 打赏
  • 举报
回复
谢谢yeefa回复。

同时感谢skyzero110,我把所有的程序都放在load中了,这样问题就解决了,也简单了,看样子还是load中比较简单。

谢谢大家的回复,感谢楼上所有的朋友!
skyzero110 2008-01-07
  • 打赏
  • 举报
回复
你这个是CellContentClick事件里写的 你设置断点 看看有没有变
其实 你可以在load中获得所有 在CellContentClick事件中 重新填充dg.Fill(ds);
dataGridView1.DataSource=ds;
yeefa 2008-01-07
  • 打赏
  • 举报
回复
this.dataGridView1.DataMember指定要显示的数据表.
yeefa 2008-01-07
  • 打赏
  • 举报
回复
Adapter.Fill(ds,"表名");

this.dataGridView1.DataSource=ds;
this.dataGridView1.DataMember="表名";
fino_wang 2008-01-07
  • 打赏
  • 举报
回复
谢谢beetle_shao的回复,还是不行,不知道问题出在哪里。一直在调试中。
谢谢大家!
fino_wang 2008-01-07
  • 打赏
  • 举报
回复
谢谢beetle_shao的回复,还是不行,不知道问题出在哪里。一直在调试中。
谢谢大家!
fino_wang 2008-01-07
  • 打赏
  • 举报
回复
谢谢beyondzxn回复!

但是按照你的说法还是不行。
beetle_shao 2008-01-07
  • 打赏
  • 举报
回复

this.dataGridView1.DataSource = ds;
改成:

if (ds!= null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
{
this.dataGridView1.DataSource = ds.Tables[0];
}
beyondzxn 2008-01-07
  • 打赏
  • 举报
回复
this.dataGridView1.DataSource = ds;
this.dataGridView.DataBind();
beyondzxn 2008-01-07
  • 打赏
  • 举报
回复
this.dataGridView.DataBind();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧