.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

creating Custom row in Gridview

Posted By: Ruchi     Posted Date: February 28, 2009    Points:2   Category :ASP.Net
I want to add at the end of grid view a new row having n textboxes for input purpose how can i do that?

Responses
Author: susant             
Posted Date: March 01, 2009     Points: 5   

Hi Ruchi

U can use footer template and add textboxes and one button to Create New it will generate textboxes and save it will save to database....

u have to write this code in source file
----------------------------------------------------
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" Width="559px">
<Columns>
<asp:TemplateField>
<FooterTemplate>
<table width="500">
<tr>
<td width="50"> <asp:TextBox ID="txt1" runat="server" Height="20%" Width="50"> </asp:TextBox></td>
<td width="100"> <asp:TextBox ID="txt2" runat="server" Height="20%" Width="100"></asp:TextBox></td>
<td width="100"> <asp:TextBox ID="txt3" runat="server" Height="20%" Width="100"></asp:TextBox> </td>
<td width="100"> <asp:TextBox ID="txt4" runat="server" Height="20%" Width="100"></asp:TextBox> </td>
<td width="10"><asp:Button ID="btn1" runat="server" Text="Save to Database" OnClick="grid_btn_click"/> </td>

<td width="10"><asp:Button ID="Button2" runat="server" Text="Add New" OnClick="create_btn"/> </td>
</tr>
</table>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

.cs file
-----------------
protected void create_btn(object sender, EventArgs e)
{
TextBox f1 = new TextBox();
f1.ID = "foter1";
GridView1.FooterRow.Cells[0].Controls.Add(f1);

TextBox f2 = new TextBox();
f2.ID = "foter2";
GridView1.Controls.Add(f2);

TextBox f3 = new TextBox();
f2.ID = "foter3";
GridView1.Controls.Add(f3);

TextBox f4 = new TextBox();
f4.ID = "foter4";
GridView1.Controls.Add(f4);
}

//u can access the textboxvalue in this way
//-----------------------------------------------
protected void grid_btn_click(object sender, EventArgs e)
{
TextBox t1 = (TextBox)GridView1.FooterRow.FindControl("txt1");
TextBox t2 = (TextBox)GridView1.FooterRow.FindControl("txt2");
TextBox t3 = (TextBox)GridView1.FooterRow.FindControl("txt3");
TextBox t4 = (TextBox)GridView1.FooterRow.FindControl("txt4");
string str = "insert into student values(" + t1.Text + ",'" + t2.Text + "','" + t3.Text + "','" + t4.Text + "') ";
}


susant
Mumbai

Author: Ruchi             
Posted Date: March 01, 2009     Points: 5   

Thank you .
I will try the code.
I was using CommandEventArgs instead of EventArgs so may be bcoz of that i was getting the textbox null.

Author: Ruchi             
Posted Date: March 02, 2009     Points: 5   

Hi Susant,
I tried your code it works as i want. But there is a problem in it i.e once i insert the record the database is updated but after that if i refresh my page then the previous record is again appended to the database.

I tried to put some validation like checking the textbox's value,etc but it doesn't work. Can you help me out ?

Thanks

Author: susant             
Posted Date: March 02, 2009     Points: 5   

Hi Ruchi

create one method show_data() it will collect all the value from database and bind to the grid..
and call that show_data() method in page load..as well save btn click event.. so in page looad event it will fire and it will collect the records from database and show in grid I think it will work fine..

And regarding validation what type of valication u want...

Few days before u send one qns about .pdf reader whether u successed on that?


susant
Mumbai



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