如何填充ASP:从code GridView的? [英] How to populate asp:GridView from code?

查看:227
本文介绍了如何填充ASP:从code GridView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表与LT;字符串[]>弥漫在我的code字符串数组项列表。在ASPX页面,我添加了一个新的网格视图控件:

I have a List<string[]> items list filled with arrays of strings in my code. On the ASPX page, I have added a new grid view control:

<asp:GridView ID="ProductList" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" EnableViewState="False">
  <Columns>
    <asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />
    <asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />
    <asp:BoundField DataField="SupplierName" HeaderText="Supplier" ReadOnly="True" SortExpression="SupplierName" />
    <asp:BoundField DataField="UnitPrice" DataFormatString="{0:C}" HeaderText="Price" HtmlEncode="False" SortExpression="UnitPrice" />
    <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
  </Columns>
</asp:GridView>

我知道我应该指定一个时尚的网格视图与此类似DataSourceId属性进行:

I know that I should specify the DataSourceID attribute for the grid view in a fashion similar to this:

<asp:GridView ... `DataSourceID="ObjectDataSource1" ... > 
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProducts" TypeName="ProductsBLL">
</asp:ObjectDataSource>

不过,我不知道该怎么办 OldValues​​ParameterFormatString SelectMethod 类型名属性重新present。另外,我没有绑定到数据库中,我只是有一个名为字符串数组项目的列表。你能帮我填充网格视图?这是没有必要贯穿装订做它。谢谢!

But, I don't know what do OldValuesParameterFormatString, SelectMethod and TypeName attributes represent. Also, I don't have the database to bind to, I just have the list of string arrays named items. Can you help me populate the grid view? It is not necessary to do it through the binding at all. Thanks!

推荐答案

您甚至不需要一个ObjectDataSource。同时在codebehind Page_Load中,就可以通过字符串数组列表解析和动态创建一个DataTable。

You don't even need an ObjectDataSource. At Page_Load in the codebehind, you can parse through the string array list and create a DataTable on the fly.

请务必将这个围绕未Page.IsPostback ,这样它不回发上重新绑定。

Make sure to wrap this around a Not Page.IsPostback so that it doesn't rebind on postback.

List<string[]> stringList = null;

DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add("ProductName", System.Type.GetType("System.String"));
dt.Columns.Add("CategoryName", System.Type.GetType("System.String"));
dt.Columns.Add("SupplierName", System.Type.GetType("System.String"));
dt.Columns.Add("UnitPrice", System.Type.GetType("System.Double"));
dt.Columns.Add("Discontinued", System.Type.GetType("System.String"));

foreach (string[] s in stringList) {
    foreach (string str in s) {
        dr = dt.NewRow();
        dr["ProductName"] = s[0];
        dr["CategoryName"] = s[1];
        dr["SupplierName"] = s[2];
        dr["UnitPrice"] = s[3];
        dr["Discontinued"] = s[4];
        dt.Rows.Add(dr);
    }
}

dt.AcceptChanges();
ProductList.DataSource = dt;
ProductList.DataBind();

这篇关于如何填充ASP:从code GridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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