0

I want to add a textBox in GridView knowing that the textbox retrieves data from a collection like ArrayList or List(OF ..) not from a dataSource (ASP.net)

Yassine edouiri
  • 281
  • 3
  • 14
  • 30

1 Answers1

0

Set the DataSource in code:

// 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();
}

Simply add a bound field column like this:

<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>

That's it

Jupaol
  • 21,107
  • 8
  • 68
  • 100