Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在GridView(ASP.NET)中显示会话变量?_C#_Asp.net_Gridview_Session Variables - Fatal编程技术网

C# 是否在GridView(ASP.NET)中显示会话变量?

C# 是否在GridView(ASP.NET)中显示会话变量?,c#,asp.net,gridview,session-variables,C#,Asp.net,Gridview,Session Variables,刚从ASP.NET开始,发现很难使用GridView。我有一组会话变量,我想将它们放入GridView控件中,但我缺乏相关知识。文件: <%@ Page Title="Warehouse" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeFile="Warehouse.aspx.cs" Inherits="Warehouse" %> <asp:Content ID

刚从ASP.NET开始,发现很难使用GridView。我有一组会话变量,我想将它们放入GridView控件中,但我缺乏相关知识。文件:

<%@ Page Title="Warehouse" Language="C#" AutoEventWireup="true" 
    MasterPageFile="~/Site.master"
    CodeFile="Warehouse.aspx.cs" Inherits="Warehouse" %>

<asp:Content ID="HeaderContent" runat="server" 
     ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Warehouse
    </h2>
    <asp:Panel ID="WarehousePanel" runat="server">
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </asp:Panel>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Warehouse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["milk"] == null)
                Session["milk"] = "0";
            if (Session["apple"] == null)
                Session["apple"] = "0";
            if (Session["orange"] == null)
                Session["orange"] = "0";

            // on page load, add session variables ie Session["milk"].ToString();
            // column 1: inventory name
            // column 2: inventory value
            GridView1.?
        }
    }

我可能认为这一切都错了。如果我这样做了,请给我指路!Thanx用于收听。

只需将其放入页面即可:

// Sample to add a value to session to ensure that something is shown
Session.Add("SessionValue1", "Value");

// Actual work of binding Session to the grid
GridView1.DataSource = Session;
GridView1.DataBind();
有一个微软公司在某种程度上回答了你的问题,因为它提供了一些数据绑定的实例,并提供了更多细节的后续文章的链接

假设您有一些代码,例如:

var warehouseItems = 
    from item in DataTableContainingWarehouseItems.AsEnumerable()
    select
    new
    {
        InventoryName = item.Field<string>("Name"),
        InventoryValue = item.Field<int>("Value"),
        InventoryPrice = item.Field<decimal>("Price"),
        StockOnHandValue = Convert.ToDecimal(item.Field<int>("Value") * item.Field<decimal>("Price"))
    };

你还没有真正取得任何进展,我建议你多读书。
GridView1.DataSource = warehouseItems;
GridView1.DataBind();