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)
            bind();
    }
    private void bind()
    {
        if (Session["tab"] == null)
        {
            DataTable table = new DataTable();
            table.Columns.Add("countryid");
            table.Columns.Add("countryname");
            DataRow dr = table.NewRow();
            dr["countryid"] = 1;
            dr["countryname"] = "uk";
            table.Rows.Add(dr);
            Session["tab"] = table;
        }
        else
        {
            
        }

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



    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    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;
        bind();
    }

and in Source:

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



 

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

Hi,

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


Thanks...


 

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:

http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx

http://www.developer.com/net/asp/article.php/3609991/Dynamic-Template-Columns-in-the-ASPNET-20-GridView-Control.htm

http://aspalliance.com/1558_Dynamic_GridView_and_DataList_in_ASPNET_20.all

Thanks,

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter