Reputation: 754
I have to insert fix number of rows to gridview. I am not using database and also not creating row one by one.
I initially want 7 rows with 3 column, with first column having text stored in my array.
I am creating gridview as,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="146px"
OnLoad="row_created" Width="308px">
<Columns>
<asp:BoundField HeaderText="Day" />
<asp:TemplateField HeaderText="Available rooms">
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Upvotes: 0
Views: 2650
Reputation: 71
I once encountered the same problem .. This tutorial helped me solve my prob .. You can check it out .. http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx
Upvotes: 0
Reputation: 548
Here is a suggested solution:
==>
public partial class WebPage : System.Web.UI.Page
{
protected DataTable GridSource
{
get { return ViewState["GridSource"] as DataTable; }
set
{
ViewState["GridSource"] = value;
gridViewControl.DataSource = value;
gridViewControl.DataBind();
}
}
private void AddRow(DataRow row)
{
// Get the lastly binded structure and data
DataTable tableSource = this.GridSource;
// Add row to data-table "tableSource"
//..
// Apply the new structure and data
this.GridSource = tableSource;
}
// .. Add relevant implementation (methods) for remove, modify operations
//..
}
Upvotes: 0
Reputation: 7150
why not creating a class object then bind it to the grid, set default values for index
to display it as Row Number
, and all other members Day, price and Rooms
filled back from user
something like:
class MyObject
{
public int Index { get; set; }
public string Day { get; set; }
public decimal Price { get; set; }
public string Rooms { get; set; }
}
use it like this
List<MyObject> lst = new List<MyObject>();
lst.Add(new MyObject { Index = 1});
lst.Add(new MyObject { Index = 2});
p.s.: Names and usage is just for explaining the idea
Upvotes: 1
Reputation: 2208
There are many ways to do that.
Some ways are either to create a DataTable and insert the data into it, and then bind it to the GridView.
A better solution would be to create objects with your data, store them in some sort of a collection(List or array will do), and then bind your collection to the DataGrid. If you don't want to create a class for this, you can use anonymous types.
Upvotes: 0