Home » Asp.netRSS

Manually Filling a Gridview

I don't want to databind a gridview, I want to be able to add rows myself within c# code.  I can see there is a GridViewRow object and wondered if I can use this to add/remove rows as required?


5 Answers Found


Answer 1

for that you need to have a datatable in session then you can add  edit items from grid view as below code:

 protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    private void bind()
        if (Session["tab"] == null)
            DataTable table = new DataTable();
            DataRow dr = table.NewRow();
            dr["countryid"] = 1;
            dr["countryname"] = "uk";
            Session["tab"] = table;

        this.GridView1.DataSource = Session["tab"] as DataTable;

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        this.GridView1.EditIndex = e.NewEditIndex;
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        TextBox box = this.GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
        DataTable tab = Session["tab"] as DataTable;
        tab.Rows[0][1] = box.Text;

        Session["tab"] = tab;

        this.GridView1.EditIndex = -1;

and in Source:

<form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="countryid"
             Width="271px" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="countryid" SortExpression="countryid">
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("countryid") %>'></asp:Label>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("countryid") %>'></asp:Label>
                <asp:TemplateField HeaderText="countryname" SortExpression="countryname">
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("countryname") %>'></asp:TextBox>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("countryname") %>'></asp:Label>


Answer 2

I will give this a try however is there no other controls whereby I can manually  enter items as a grid.  The ListBox, RadioList, CheckBoxList, DropDownList etc etc all allow for this.


Answer 3


There are many ways to do one thing but it depends upon your strength and also your desired output that what you want exactly?

you can impliment ListBox, RadioList, CheckBoxList, DropDownList etc easily inside grid..

or you can think for create control at run time..........and then do whatever you want.....



Answer 4

I simply wanted to let the user select items from one list that were then inserted into the grid and then use the items in the grid to process next action.  Some of the columns in the grid would need to be unbound i.e. for checkboxes etc which is why I don't want to databind  it.


Answer 5

Hi breath2k,

First of all, to show GridView, you have to bind a datasource to it, no matter it is a null DataTable or SqlDataSource. With the data source, then you can generate the GridView dynamically with adding template manually. Please refer:






<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter