.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to add control dynamically in Gridview

Posted By: Ramesh     Posted Date: April 20, 2011    Points:5   Category :ASP.Net
Hi,

How to add control dynamically in Gridview in asp.net. I want to add Textbox and CheckBox controls programmatically inside Gridview when user click on add new row button.

Regards,
Ramesh

Responses
Author: shankey             
Posted Date: April 21, 2011     Points: 20   

hi,
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 Employee Name, Gender and Country.

(For ItemTemplates, labels are used to display data in non edit mode as per convention.)


<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="EmployeeName" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="FooterEmployeeName" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="EmployeeGender" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="FooterEmployeeGender" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="EmployeeCountry" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="FooterEmployeeCountry" 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()


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 EmployeerName = ((TextBox)gvwUsers.FooterRow.FindControl("FooterEmployeeName")).Text;
string EmployeeSex = ((DropDownList)gvwUsers.FooterRow.FindControl("FooterEmployeeSex")).SelectedItem.Text;
string EmployeeCountry = ((DropDownList)gvwUsers.FooterRow.FindControl("FooterEmployeeCountry")).SelectedItem.Text;

// now write ur code to insert the data to database
//after that rebind the GridView

BindGrid();
SetVisibilities(false);

Hope this helps!

Thanks

Shankey


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend