Adding a new row in the gridview on click of add button..

Hi,

I have a gridview which gets populated by calling BLL method . My BLL returns Datatable and am binding it to the gridview.Now i want to add new row.For that i have written aspx as below:

 

<asp:GridView ID="gvSubContractor" runat="server" AutoGenerateColumns="False" EmptyDataText="No Data"

Width="959px" ShowFooter="false" OnRowCreated="gvSubContractor_RowCreated"

onrowcommand="gvSubContractor_RowCommand">

<RowStyle CssClass="ItemStyle" />

<Columns>

<asp:TemplateField HeaderText="ContractorName" >

<FooterTemplate>

Enter Contractor Name:

<asp:TextBox runat="server" ID="InsertContractorName" Text='<%# Bind("ContractorName") %>' />

</FooterTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtContractorName" runat="server" Text='<%# Bind("ContractorName") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<%# Eval("ContractorName")%>

</ItemTemplate>

<FooterTemplate>

<asp:Button runat="server" ID="Insert" Text="Add SubContractor" CommandName="InsertNew" />

</FooterTemplate>

</asp:TemplateField>

<asp:BoundField DataField="ContractorPIName" HeaderText="PI Name" />

<asp:BoundField DataField="ContractorPIEmail" HeaderText="PI Email" />

<asp:BoundField DataField="ContractorPIPhone" HeaderText="PI Phone" />

<asp:CommandField ShowEditButton="True" />

<asp:CommandField ShowDeleteButton="True" />

 

</Columns>

<EmptyDataTemplate>

Contractor Name:<asp:TextBox runat="server" ID="NoContractorName" />

<asp:Button runat="server" ID="NoDataInsert" CommandName="NoDataInsert" Text="Insert" />

</EmptyDataTemplate>

 

<HeaderStyle CssClass="HeaderStyle" />

<AlternatingRowStyle CssClass="AlternatingStyle" />

</asp:GridView>

 

Now on click of Add button am writing the below code: At present it just adds empty row.

protected void btnAddSubContractor_Click(object sender, EventArgs e)

{

int TaskOrderID = 3;

DataTable dt = TaskOrderManager.GetTaskOrderContractor(TaskOrderID);

dt = (DataTable)gvSubContractor.DataSource;

DataRow dr = dt.NewRow();

//make headertemplate visible to edit

gvSubContractor.ShowFooter = true;

// dr["ContractorName"] = ContractorName.Text;

//dr["ContractorName"]= txtCName.Text ;

dt.Rows.Add(dr);

gvSubContractor.DataSource = dt;

gvSubContractor.DataBind();

}

 

But i want to add row without using sqldatasource or objectdatasource control.Please tell me how can i add it?

0
Setty
4/15/2009 7:29:54 PM
asp.net.visual-studio-2005 7760 articles. 0 followers. Follow

2 Replies
862 Views

Similar Articles

[PageSpeed] 53

Setty:
But i want to add row without using sqldatasource or objectdatasource control.Please tell me how can i add it?
 

Here's an example on how to add rows in GridView without using those wizard datasources..

ASPX:

 

<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:TextBox ID="TextBox3" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
  

CODE BEHIND:

 

//A method that will BIND the GridView based on the TextBox 
//values and retain its values on post backs.


private void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("TextBox2Column", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("TextBox3Column", typeof(String)));

if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();

}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);

}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;

dt.Rows.Add(dr);

}

// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();

}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;

}

protected void Button1_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
TextBox1.Text = string.Empty;

TextBox1.Focus();
}
  

You can refer here for more information.

Adding Multiple Columns and Rows in GridView without using a Database

 

You might be also interested on this:

Adding Multiple Rows in GridView


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
4/15/2009 9:42:51 PM
Thanks and it is working great.
0
Setty
4/16/2009 6:28:45 PM
Reply:

Similar Artilces:

How i add New row to gridview when i click Add button
Hi, I have Gridview control , it is populated from Products Table from my database ProductNo,ProductName, ProductPrice, PrductCategorytype I added Edit and delete buttons to Gridview. Here  according to my user requirement  , i shuld add ADD button outside Griedview contrl, for Inserting New records into database  when i click ADD button But here  How i add New row to gridview when i click Add buttonadilahmed  http://geekswithblogs.net/casualjim/articles/51360.aspx  Thanks and RegardsS.K.Vignesh----------------------Do remember to "Mark as answe...

add new rows in gridview on button click
Hi , I am designing a gridview.In that i have a add button on the header and edit /update/cancel for all rows.If i click add button i have to insert new empty row( which has 2 textboxes,2 dropdownboxes=total 4 columns)  on the bottom of the gridview with no edit/update/cancel Can anyone let me know how to do this.I use rowcommand and find out command name and i am calling afunction   like <asp:gridview id="gridview1" onrowupdating ="row_update" onrowediting="row_edit" onrowcancelingedit="canceledit" onrowcommand="addemptyrow&q...

Add a button to add a new row and insert the new record to gridview
Hello, I have a gridview1 on a page attached to a SQLDataSource.  The gridvioew page is displayed when a selection from a drop down box on the master page matches the FK field (partID) on the gridview page via querystring.  I would like to add a buttton that allows me to add a row to gridview1 without losing the information already displayed, typed in, or updated.  I have the PK and 8 check box feilds set as bound field (DetailID) and all of the remaining 10 text fields converted to template fields.  What is the best way to add this functio...

Add new row to a binded gridview on button clicked?
 Hi all,I have a GridView that binded to some data in SQL database. (see image) What I'm trying to accomplish is that when I click on the "Add Lot" button, a new row WITH THE EXACT COLUMNS of the clicked button is insert RIGHT BELOW that row. So for example, if I click the "Add Lot" button on the first row (with SKU 4942718), a new Row will be inserted right below it, with the exact same information (SKU, a textbox for LOT #, another column with a textbox / dropdownlist / add lot button.) I'm relative new to .net so any help would be greatly appreciated....

How can we add a new row to the gridview in the button click event
I have a button,3 textboxes and a gridview.Iam adding the textboxes to the gridview and iam binding a datatable . Now my question is that how can i add a new row to my gridview in the button click event. in the button click event, if u update the datatable, the grid view should read/refresh  the new rows into the page. what are u thring to  do , you want to do this thorguh to UI . . if so what is the use of text boxes.  Sunil Okay that's not difficult but the consequences are. you may need to write a good deal of code to add row manange the user created row.&...

does anyone know how to do that, when I click the last row of the gridview, the gridview will automatically add an empty new row for me.
does anyone know how to do that, when I click the last row of the gridview, the gridview will automatically add an empty new row for me.   thanks hi there,the only I could think of for now is is enbale footer rowthen hide it onpageload so when last row click set footer.visible to true  ...

Getting an error to add a new row when a button is clicked inside a gridview
Here is the source code default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dgnewrow.aspx.cs" Inherits="dgnewrow" enableEventValidation="True"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"><title>Untitled Page</title> </head> <body> <form id="form1" runat="server">...

Visual Studio 2005 Edition Released! Is it the same as Visual Studio.Net 2005 ?
Edited by XIIIVisual Studio 2005 Edition Released! Is it the same as Visual Studio.Net 2005 ? Yes. ".NET" part is not actually/officially part of the Visual Studio 2005 name.-- Mike PopeASP.NET User EducationThis posting is provided "AS IS" with no warranties, and confers no rights. Is it Visual Studio .NET 2003 update version? Does Microsoft change its name? I'm not sure I understand your question exactly -- I'm a little thrown by the word "update." Visual Studio 2005 is the new/current version, released on Monday. This product never officially had the word ".NET" in its nam...

Gridview does not display the new record after I click the Add button and the record is added to the DB.
HI  page: VS 2005 & vb.net I have an aspx page that has a Gridview control with the footer row enabled for the purpose of adding a blank row at the bottom of the gridview that is used to add new records to the db table.  I have a template field with the Add linkbutton control that is used by the end-user to click on when they finished inserting the new information they want added.  I placed an datasource control on the page that the Add buton uses to insert the new information below: Problem: When I click the Add button, the data is added to the DB but I have ...

Adding row (one at a time) in gridview on a button click. And cells of added row should contain controls like DROPDOWN LIST or TEXT BOX.
Dear Friends, I am in great need for the solution/hints/guidance of my requirement, if anyone can help with.  Let me explain what I need."I need a Gridview control on a webpage having following 5 columns/fields.     Column 1: It just has a "+" button in its header only (other cells for this column is empty)    Column 2: The Column header here is "FIELD" and  it should contain dropdown list (which is populated from some database)    Column 3: The Column header here is "Operator" and  it should ...

when ever a user click on add button a new row with another dropdown lists should be added to an existing table
Hi everyone, I am adding a Table(server control)  to a usercontrol . the table consists a row containing drop down lists with add button . when ever a user click on add button a new row with another dropdown lists should be added.is there any way to copy existing row? so that it uses AJAX not page post back .  I tried to a add new row in button click event to the table. even though a row is showing up on the user control as in pre-render  but it is not adding to the table i intended to add when the page is post backed i.e when the page is reloaded it shows a initial ...

Loading Visual Studio.net 2003 on a pc with Visual Studio.net 2005 installed
I have recently installed Visual Studio.net 2005 on a pc and have been using it to create a .net 2.0 application. I also have pc running Visual Studio 2003 that I use to support a .net 1.1 application. This pc has broke. Would I be ok loading Visual Studio 2003 on the machine I have running Visual Studio 2005 so I can manage both the .net 1.1 and the .net 2.0 applications?               Thanks Danny Hmmm. Not sure if it works in that direction, but I installed VS2005 on a machine that has VS2003 already installed and they se...

can we migrate visual studio .net 2003 project to visual studio.net 2005
Hai friends, I am doing project in ASP.NET(vb language)  I have visual studio .net 2003 installed in my pc and i am doing my project in it , because i need to use additional functionality like "AJAX" , i want to use visual studio .net 2005 or visual studio .net 2008 my project folder name is "sqls" can i install visual studio .net 2005 or visual studio .net 2008 and migrate this project folder to work in visual studio .net 2005 or visual studio .net 2008 i have uninstall visual studio .net 2003 and install visual studio .net 2005 or visual ...

AxCMS.net Award 2005 | 1st place: MSDN Professional subscription including Visual Studio 2005 and SQL Server 2005; 2nd place: new Xbox 360
We would like to introduce the 100% cost free AxCMS.net 5.2.2 - brand new release! AxCMS.net is a 100% cost free Enterprise CMS platform for architects and professional developers based 100% on Microsoft .NET. With the new download version we offer ... Improved user interface (thank you for feedback!) International support avaliable! New features (muitisite, permissions, authentication, newsletter, ...) Focused on software architects and professional developers Introducing AxCMS.net Award 2005! The AxCMS.net Award 2005 represent an opportunity ...

Web resources about - Adding a new row in the gridview on click of add button.. - asp.net.visual-studio-2005

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/8/2016 8:17:51 AM