如何绑定网格视图 [英] How to bind a grid view

查看:73
本文介绍了如何绑定网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

如何绑定网格视图.
我的代码如下.
但是它没有显示网格.

请帮助

Hello

How to bind a grid view.
My code is as follows.
But it is not displaying a grid.

Plz help

protected void Page_Load(object sender, EventArgs e)
        {            
            //string connection="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True";
            string connection = "Data Source=path;Initial Catalog=commonlogin;User ID=sa;Password=; ";

            SqlConnection conn = new SqlConnection(connection);
            conn.Open();
            SqlCommand com = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);

            SqlDataReader reader = com.ExecuteReader();
            
            GridView1.DataSource = reader;
            GridView1.DataBind();
        }


-------------------------
请建议我正确的代码


-------------------------
Plz suggest me correct code

推荐答案

尝试一下:
try this :
protected void Page_Load(object sender, EventArgs e)
{

//string connection="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True";
string connection = "Data Source=path;Initial Catalog=commonlogin;User ID=sa;Password=; ";
 

SqlConnection conn = new SqlConnection(connection);
conn.Open();
SqlDataAdapter ad= new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
Dataset ds=new Dataset();
ad.fill(ds);

if(ds.tables[0].rows.count!=0)
{
          Gridview1.Datasource=ds;
          Gridview1.Databind();
}
Hope this will help you or if not Please the Post error which is generated.
Enjoy.


SqlCommand com = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);

 SqlDataAdapter adap = new SqlDataAdapter(com );
            DataTable dt = new DataTable();
            adap.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();



您可以在此链接中看到一个简单的示例


C#SqlDataAdapter



you can see an easy example in this link


C# SqlDataAdapter


网格包含什么?
我的意思是您是自动生成列还是已指定要显示的列.
您可能正在尝试获取查询未返回的列
请在gridview中尝试AutoGenerateColumns ="true",这将使您能够检查gridview是否正确绑定.
接下来,您可以指定需要与标题一起显示的列.

希望这对您有所帮助.
编码愉快!
干杯
What does your grid contain?
I mean are you autogenerating columns or have you specified the columns to be displayed.
You might be trying to get a column that the query is not returning
Please try AutoGenerateColumns="true" in gridview, this will enable you to check if the gridview is bound properly or not.
Next you can specify the columns you need to display along with the headers.

Hope this helps you out.
happy coding!
Cheers


这篇关于如何绑定网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆