当前位置: 代码迷 >> C# >> Gridview 和DataSource 手动配置好后怎么在页面打开时就能显示数据
  详细解决方案

Gridview 和DataSource 手动配置好后怎么在页面打开时就能显示数据

热度:397   发布时间:2016-05-05 05:31:40.0
Gridview 和DataSource 手动配置好后如何在页面打开时就能显示数据?
本帖最后由 xpxpxpxp 于 2014-12-15 10:23:47 编辑
这是我配置的情况,测试查询的时候能出来数据,但是怎么调用呢?在pageload中写DataBind不起作用,谢谢各位大侠




------解决思路----------------------

  string strConn = "data source=192.168.1.2;initial catalog=work;user id=sa;password=sa";
            SqlConnection conn = new SqlConnection(strConn);
            string strSQL = "select *  from data";
            SqlDataAdapter rst = new SqlDataAdapter(strSQL, conn);
            conn.Open();
            DataSet ds = new DataSet();
            rst.Fill(ds);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();




------解决思路----------------------
点击DataGrid,绑定数据源,选择你配置的这个数据源就好了。
------解决思路----------------------
1.那你先把你数据源里面的所有条件去掉,看能出来数据吗?
2.如果能出来数据,说明是你的数据源里面写的条件有问题,逐条去排查条件。观察Session的值。
------解决思路----------------------
引用:
Quote: 引用:

1.那你先把你数据源里面的所有条件去掉,看能出来数据吗?
2.如果能出来数据,说明是你的数据源里面写的条件有问题,逐条去排查条件。观察Session的值。

测试查询数据都没问题,我把页面的代码贴一下,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class kaguanyuan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataBind();

        Label1.Text = (Session["dengluS"] as userinfo).Xingming;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

1.那你先把你数据源里面的所有条件去掉,看能出来数据吗?,这个问题你还没回答我。
------解决思路----------------------
仔细检查你的session
------解决思路----------------------
引用:
谢谢各位,确实是session有问题,我是把一个实体类封装在session里面了,然后where条件要用到其中的一个字段,是不是不支持这样啊?我现在用了另外的方法,先把用到的字段赋给一个lable,然后再取他的值,之前直接取session类里面的字段我是这么写的,但是不成功,不知道该怎么写??

在Page_Load的时候
先赋值,
Label1.Text = (Session["dengluS"] as userinfo).Xingming;
再绑定
GridView1.DataBind();
  相关解决方案