Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a grid view with two columns, the first one is a template field containing an Image, the second column is a template field containing a radiobutton.

I have a button when clicked I want to Add new row with a new image, I want to do this via code at run time i tried
MyGridView.Rows.Add();
but it's not working, a compilation error comes up.
below the code:
ASP.NET
<asp:GridView ID="grd" runat="server" CellPadding="20" ForeColor="#333333" GridLines="None" CellSpacing="10" AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/logo.png" />
    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Main">
                <ItemTemplate>
                    <asp:RadioButton ID="rbt" runat="server" GroupName="Main" OnClick="javascript:Selrdbtn(this.id)" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="add" />
Posted
Comments
Dheeraj_Gupta 17-Jul-13 5:16am    
can you provide the error...
Hend Riad 17-Jul-13 5:22am    
'System.Web.UI.WebControls.GridViewRowCollection' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRowCollection' could not be found.

As per your requirement.

Please refer following link..

NewRow GridView[^]

Mark it as answer if suffice your query.
 
Share this answer
 
Comments
Hend Riad 17-Jul-13 5:34am    
Thanks for your reply, but it's complicated, is there another way simple more to add new row at run time ?
Dheeraj_Gupta 17-Jul-13 5:50am    
Dear there is no method like this.
MyGridView.Rows.Add();

instead you need to pass the GridViewRow object to the Add method.
Refer - Adding a New Row in GridView[^].

It gives a simple example of adding a row to GridView.
Please follow that and make necessary modification to complete your task.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900