0

我想在 GridView 中添加一个文本框,知道文本框从 ArrayList 或 List(OF ..) 等集合中检索数据,而不是从数据源 (ASP.net)

4

1 回答 1

0

在代码中设置数据源:

// in this case the object I am using is:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public bool IsInStock { get; set; }
}

private void BindGrid()
{
   // in the GetProducts() method return the collection
   List<Product> r = GetProducts();
   this.grdSomething.DataSource = r;
   this.grdSomething.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    this.BindGrid();
}

protected void grdSomething_EditCommand(object sender, DataGridCommandEventArgs e)
{
    this.grdSomething.EditItemIndex = e.Item.ItemIndex;
    this.BindGrid();
}

只需添加一个绑定字段列,如下所示:

<asp:DataGrid runat="server" ID="grdSomething" AutoGenerateColumns="False"
    OnEditCommand="grdSomething_EditCommand">
    <Columns>
       <asp:BoundColumn DataField="Name"></asp:BoundColumn>
       <asp:EditCommandColumn EditText="Edit"></asp:EditCommandColumn>
    </Columns>
</asp:DataGrid>

而已

于 2012-06-14T23:47:33.533 回答