我在页面中创建了一个GridView,前台代码是
 <asp:GridView ID="GvDataList" runat="server" CellPadding="4" 
        ForeColor="#333333"  AutoGenerateColumns="false" Width="100%" 
        Height="25px" AllowPaging="True" 
        GridLines="None" Font-Size="12px" 
        onpageindexchanging="GvDataList_PageIndexChanging" PageSize="11">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="SiteID" HeaderText="站点号" />
            <asp:BoundField DataField="SiteName" HeaderText="站点名称" />
            <asp:BoundField DataField="ShuiWei" HeaderText="水位" />
            <asp:BoundField DataField="Liuliang" HeaderText="瞬时流量" />
            <asp:BoundField DataField="LiuLiang_Total" HeaderText="累计流量" />
            <asp:BoundField DataField="TodayYL" HeaderText="雨量" />
            <asp:BoundField DataField="Date_Time" HeaderText="时间" />
             <asp:BoundField DataField="MYL" HeaderText="月雨量" />
           <asp:BoundField DataField="YYL" HeaderText="年雨量" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
它的数据来自于下面的存储过程SelectCurrentData的运行USE [GQ-QiaoYing]
GO
/****** Object:  StoredProcedure [dbo].[SelectCurrentData]    Script Date: 07/04/2012 20:01:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SelectCurrentData]
as
select SiteID,SiteName from SiteInfo order by SiteID
SELECT  ShuiWei,LiuLiang,LiuLiang_Total,YuLiang,Date_Time FROM LastData order by SiteID
select  SUM(YuLiang)as MYL from HistoryData group by convert(varchar(7), Date_Time, 120),SiteID having convert(varchar(7), Date_Time, 120)=(select MAX(CONVERT(varchar(7), Date_Time, 120)) from HistoryData)order by SiteID
select  SUM(YuLiang)as YYL from HistoryData group by convert(varchar(4), Date_Time, 120),SiteID having convert(varchar(4), Date_Time, 120)=(select MAX(CONVERT(varchar(4), Date_Time, 120)) from HistoryData)order by SiteID
.Net后台代码为:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            bind();        }
    }
    private void bind()
    {
        using (SqlConnection con = DB.getstrConn())
        {
            con.Open();
              using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "SelectCurrentData";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                GvDataList.DataSource = ds;
                GvDataList.DataBind();            }
            
        }
运行后,发现查找不到ShuiWei等字段
错误如下:
在选定的数据源上未找到名为“ShuiWei”的字段或属性。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 在选定的数据源上未找到名为“ShuiWei”的字段或属性。源错误: 
行 43:                 adapter.Fill(ds);
行 44:                 GvDataList.DataSource = ds;
行 45:                 GvDataList.DataBind();
行 46: 
行 47:        
 
也许是GridView无法找到table[1]、table[2]‘table[3]的字段,只能找到table[0]的字段,所以请问各位大侠,如何才能让GridView找到所有表的字段,查询结果的表结构是不相同的

解决方案 »

  1.   


    //先修改下你的存储过程,最后选出时是一条选出语句,不要四条
    //然后后台改下
    GvDataList.DataSource = ds.Tables[0];
    --你这句是想做什么的,感觉有点问题,你想要的结果是什么,应该很简单的语句就搞定的
    --看你的意思是按年求和,呵呵
    select SUM(YuLiang)as YYL from HistoryData group by convert(varchar(4), Date_Time, 120),SiteID having convert(varchar(4), Date_Time, 120)=(select MAX(CONVERT(varchar(4), Date_Time, 120)) from HistoryData)order by SiteID
      

  2.   

    GvDataList.DataSource = ds;
    只绑定了第一个表,也就是 ds.Tables[0]你组织下你的sql语句,一条就返回所有需要的字段
      

  3.   

    我组织过了,用的是内联接inner join...on,但是执行超时
      

  4.   

    超时和 数据量有关系吧 看看SQL 执行多长时间
      

  5.   

    先从你的存储过程的SQL语句着手进行优化处理
      

  6.   

    楼主看下面我的例子,你可以尝试一下啊这种方法!public DataTable User_Sel()
            {
                try
                {
                    string str = "select * from Users,Department where did=dptid  and uname!='admin'";
                    cmd = SqlHelper.CreateDbCommand(str, conn);
                    conn.Open();
                    DataTable dt = new DataTable();
                    dt.Load(cmd.ExecuteReader());
                    return dt;
                }
                catch (Exception e)
                {                throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
     public void UserBind()
            {
                Users user = new Users();
                GridView1.DataSource = user.User_Sel();
                GridView1.DataBind();
            }
      

  7.   

    楼主 你的SQL 真是博大精深啊。
    还是先改改SQL吧 不会的就发出来问,会有人帮你解答的。