我是C#的新手,不知道如何在我的.aspx页面中使用C#gridview进行显示。你怎么编码呢?下面是我做的代码,但我需要一些建议,看它是否正确。
这是我在.aspx页面中的代码
<asp:GridView ID="WorkshopEnrolled" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="WorkshopEnrolledDataSource" EnableViewState="False">
<Columns>
<asp:BoundField DataField="WorkshopCode" HeaderText="Workshop Code"
SortExpression="WorkshopCode" />
<asp:BoundField DataField="WorkshopName" HeaderText="Workshop Name"
HtmlEncode="False" SortExpression="WorkshopName">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="WorkshopDate"
HeaderText="Workshop Date" SortExpression="WorkshopDate">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="WorkshopLocation"
HeaderText="Workshop Location" SortExpression="WorkshopLocation">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</Columns>
这是我的.aspx.cs文件
public partial class Workshop : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie httpCookie = Request.Cookies["TeamAwesomeCookie"];
if (httpCookie != null)
{
// Write code to update photo filename in Users' table
WorkshopController wc = new WorkshopController();
ArrayList workshopEnrolled = wc.retrieveWorkshopByStudent(int.Parse(httpCookie.Values["UserID"]));
ArrayList availableWorkshop = wc.retrieveAvailableWorkshops();
}
}
protected void WorkshopEnrolled_RowDataBound(object sender, GridViewRowEventArgs e)
{
//string workshopCode = null;
// string workshopName = null;
// string workshopDate = null;
// string workshopLocation = null;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Reference the ProductsRow via the e.Row.DataItem property
Workshop workshopEnrolled = (Workshop)e.Row.DataItem;
}
}
}
答案 0 :(得分:1)
乍一看,您的代码看起来不错,只是您忘记了DataBind您的网格,而您似乎的目的是绑定来自代码的数据。话虽如此,删除
DataSourceID="WorkshopEnrolledDataSource"
从标记中将这些行添加到代码后面,在2个ArrayList声明下面:
WorkshopEnrolled.DataSource= workshopEnrolled ;
WorkshopEnrolled.DataBind();
答案 1 :(得分:0)
我通过Google找到了article here,它有一些实用GridView的实用步骤。