Heena
Heena

Reputation: 754

how to add fix number of rows to gridview in c#?

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

Answers (4)

toolmaster
toolmaster

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

Tathagat Verma
Tathagat Verma

Reputation: 548

Here is a suggested solution:

  • Define the data-source for the grid as a data-table
  • And just change the structure of the data-table object and rebind to the grid, as applicable
  • Example:-

==>

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

Rami Alshareef
Rami Alshareef

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

Svarog
Svarog

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

Related Questions