I suppose you want to add a new row.

I suppose you want to add a new row.

To achieve this we normally use templates.

  • ItemTemplate to display values
  • EditItemTemplate to edit values
  • FooterTemplate/InsertItemTemplate to insert values

Suppose the three fields in the GridView are Name, Sex and Country.
(Here I am placing labels at ItemTemplates as per convention.)

So, the markup of theGridView will be like this.

 

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="UserName" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="FooterUserName" runat="server">
        </asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="UserSex" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:DropDownList ID="FooterUserSex" runat="server">
        </asp:DropDownList>
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="UserCountry" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:DropDownList ID="FooterUserCountry" runat="server">
        </asp:DropDownList>
    </FooterTemplate>
</asp:TemplateField>

 

Steps for Adding a New Row at a Button Click
1. Place a “Add New Row” button outside the gridview.(id=”btnAddNewRow” Text=”Add New Row”)
2. Place a Insert Button(id=”btnInsert” Text=”Insert”) and Cancel Button(id=”btnCancel” Text=”Cancel”) next to btnAddNewRow.
3. Set Visible = “false” for Insert Button and Cancel button
4. Create a function SetVisibilities()

 

Steps for Adding a New Row at a Button Click

1. Place a “Add New Row” button outside the gridview.(id=”btnAddNewRow” Text=”Add New Row”)
2. Place a Insert Button(id=”btnInsert” Text=”Insert”) and Cancel Button(id=”btnCancel” Text=”Cancel”) next to btnAddNewRow.
3. Set Visible = “false” for Insert Button and Cancel button
4. Create a function SetVisibilities()

 

private void SetVisibilities(bool isVisible)
{
        btnAddNewRow.Visible = !isVisible;
        GridView1.FooterRow.Visible = isVisible;
        btnInsert.Visible = isVisible;
        btnCancel.Visible = isVisible;
}

 

5. Inside the Add New Row Buttons(btnAddNewRow) OnClick event
string userName = ((TextBox)gvwUsers.FooterRow.FindControl("FooterUserName")).Text;
string userSex = ((DropDownList)gvwUsers.FooterRow.FindControl("FooterUserSex")).SelectedItem.Text;
string userCountry = ((DropDownList)gvwUsers.FooterRow.FindControl("FooterUserCountry")).SelectedItem.Text;
// now write ur code to insert the data to database
//after that rebind the GridView
BindGrid();
SetVisibilities(false);

Hope this helps!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s