User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 455,967 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,740 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 8314 | Replies: 8
Reply
Join Date: Aug 2006
Posts: 7
Reputation: karthikcyano is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
karthikcyano karthikcyano is offline Offline
Newbie Poster

Question Adding a new row in a Datagrid ???????????

  #1  
Nov 26th, 2007
Hi,

I had 2 Template columns in a Datagrid, each column contains a Textbox. The user can enter the values and it should be saved. I wanna add a new row if a user clicks add new button. After Entering the values in the textboxes (inside the Datagrid) if he clicks save button, the record should be saved. I need the code for doing this in ASP.NET(c#). Can anyone suggest me !!!!!!!!!!!!!!!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 99
Reputation: edek is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster in Training

Re: Adding a new row in a Datagrid ???????????

  #2  
Nov 26th, 2007
If by "each column contains a Textbox." you mean that there is a textbox outside the Datagrid object, where user can input values, and you want to add a new row filled with data from those textboxes, you can do it like that:

//You should already have a dataset binded with data grid, right? Let say that your                  //dataset's name is 'ds':
            DataSet ds = new DataSet();
            ds.Tables.Add("MyTable");

//create row and fill it:      
            DataRow rw = new DataRow();

            rw["MyColumn1"] = textBox1.Text;
            rw["MyColumn2"] = textBox2.Text;
            rw["MyColumn3"] = textBox3.Text;
            rw["MyCol........

//add the row:
            ds.Tables["MyTable"].Rows.Add(rw);

Changes are automatically included in your dataset (here named 'ds').
How to save changes to database depends on how you are connected to it, so I sugest reading about it on your DB producer's site.

Hope it helped!
Last edited by edek : Nov 26th, 2007 at 6:57 am.
Reply With Quote  
Join Date: Aug 2006
Posts: 7
Reputation: karthikcyano is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
karthikcyano karthikcyano is offline Offline
Newbie Poster

Question Re: Adding a new row in a Datagrid ???????????

  #3  
Dec 3rd, 2007
Originally Posted by edek View Post
If by "each column contains a Textbox." you mean that there is a textbox outside the Datagrid object, where user can input values, and you want to add a new row filled with data from those textboxes, you can do it like that:

//You should already have a dataset binded with data grid, right? Let say that your                  //dataset's name is 'ds':
            DataSet ds = new DataSet();
            ds.Tables.Add("MyTable");

//create row and fill it:      
            DataRow rw = new DataRow();

            rw["MyColumn1"] = textBox1.Text;
            rw["MyColumn2"] = textBox2.Text;
            rw["MyColumn3"] = textBox3.Text;
            rw["MyCol........

//add the row:
            ds.Tables["MyTable"].Rows.Add(rw);

Changes are automatically included in your dataset (here named 'ds').
How to save changes to database depends on how you are connected to it, so I sugest reading about it on your DB producer's site.

Hope it helped!





Hi,

The Textboxes are inside the datagrid. Assume u want to enter a timesheet for a week so there ll be 7 days (i.e 7 textboxes). In this he can choose different projects and enter the no of hours in the textboxes (inside the grid) for a day. For tat he ll be adding another row. I want to know how to add the next row of Textboxes inside the grid without refreshing the page, and how to track the values. Hope u got my question fully. Give a suggestion for this in C#.Net (Web Application)

Thanks in advance
Reply With Quote  
Join Date: Aug 2006
Posts: 7
Reputation: karthikcyano is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
karthikcyano karthikcyano is offline Offline
Newbie Poster

Re: Adding a new row in a Datagrid ???????????

  #4  
Dec 18th, 2007
Thanks Edek, It worked for me...





Originally Posted by karthikcyano View Post
Hi,

The Textboxes are inside the datagrid. Assume u want to enter a timesheet for a week so there ll be 7 days (i.e 7 textboxes). In this he can choose different projects and enter the no of hours in the textboxes (inside the grid) for a day. For tat he ll be adding another row. I want to know how to add the next row of Textboxes inside the grid without refreshing the page, and how to track the values. Hope u got my question fully. Give a suggestion for this in C#.Net (Web Application)

Thanks in advance
Reply With Quote  
Join Date: Feb 2008
Posts: 2
Reputation: maluisalea is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
maluisalea maluisalea is offline Offline
Newbie Poster

Solution Re: Adding a new row in a Datagrid ???????????

  #5  
Feb 29th, 2008
use dataset and add a blank row in it when you click the add new row button then bind it to the datagid. Make that new row in edit mode and change the update button text property to "Save" so when you click it, instead of updating, it will insert a new record on the database. You can have a tag on the update event of your datagrid to test whether you are inserting or updating.

http://fragilegirl-devblog.blogspot....atagrid-c.html
Reply With Quote  
Join Date: Nov 2007
Location: California
Posts: 18
Reputation: shaulf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
shaulf's Avatar
shaulf shaulf is offline Offline
Newbie Poster

Re: Adding a new row in a Datagrid ???????????

  #6  
Nov 29th, 2007
I would also use Table.AcceptChanges(); after the row was added...
Reply With Quote  
Join Date: Aug 2008
Posts: 1
Reputation: raghvendra.mask is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
raghvendra.mask raghvendra.mask is offline Offline
Newbie Poster

Re: Adding a new row in a Datagrid ???????????

  #7  
Aug 16th, 2008
Hi

I do have same problem as u do have .could u please tell me have u got the salution for this problem
if yes please help me too.
Reply With Quote  
Join Date: Aug 2008
Posts: 2
Reputation: oscprofessional is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
oscprofessional's Avatar
oscprofessional oscprofessional is offline Offline
Newbie Poster

Re: Adding a new row in a Datagrid ???????????

  #8  
Aug 18th, 2008
Hi,

this may help u....

Defaultly you can edit the values in the datagrid unless you disable the grid. So the

problem here lies on the delete and add. In the add button you can directly add a newrow to

your datasource and it will be reflected on your datagrid. Same also with delete:

// On your AddButton_Click event
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
dataGrid1.CurrentRowIndex = dt.Rows.Count - 1;

// On your DeleteButton_Click event
dt.Rows.RemoveAt(dataGrid1.CurrentRowIndex);

(Where dt is a datatable object)
if this post is help ful then mark it as an answer & mark it .......
Last edited by oscprofessional : Aug 18th, 2008 at 5:06 am.
Reply With Quote  
Join Date: Sep 2008
Posts: 1
Reputation: sabeerpasha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sabeerpasha sabeerpasha is offline Offline
Newbie Poster

Re: Adding a new row in a Datagrid ???????????

  #9  
Sep 14th, 2008
Originally Posted by karthikcyano View Post
Hi,

I had 2 Template columns in a Datagrid, each column contains a Textbox. The user can enter the values and it should be saved. I wanna add a new row if a user clicks add new button. After Entering the values in the textboxes (inside the Datagrid) if he clicks save button, the record should be saved. I need the code for doing this in ASP.NET(c#). Can anyone suggest me !!!!!!!!!!!!!!!!!


Here is One solution for This. code this in Design Page
<asp:TemplateColumn HeaderText="Staff Id" ItemStyle-CssClass="gridItem" HeaderStyle-CssClass="gridHead">
<ItemStyle Width="10%"></ItemStyle>
<ItemTemplate>
<asp:Label Width="70px" ID="lblStaffId" Runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStaffId" CssClass="txtbox" Width="70px" MaxLength="50" Runat="server" AutoPostBack="True" OnTextChanged="txtStaffId_TextChanged"></asp:TextBox>
</EditItemTemplate> </asp:TemplateColumn>

For adding new row to datagrid. add it in .cs file
public DataTable dtbtable=new DataTable();
ViewState["mode"] = "add";
dtbtable.Rows.Add(dtbtable.NewRow());
this.dgDetails1.EditItemIndex = dtbtable.Rows.Count - 1;
dgDetails1.DataSource = dtbtable;
dgDetails1.DataBind();
The Above Code will add a new Row with TextBoxes you can enter in Textboxes.

the Following code will find the text entered by U.
string strStaffId = "";string strIsAdmin = "";string strDisUsr = ""; string strIchUsr = "";
strStaffId = ((TextBox) e.Item.FindControl("txtStaffId")).Text.ToLower();
strIsAdmin= ((YesNo) e.Item.FindControl("YesnoAdminUC")).SelectedOption;
strDisUsr = ((DropDownList) e.Item.FindControl("cmbDiscrepency")).SelectedItem.Value;
strIchUsr = ((DropDownList)e.Item.FindControl("cmbICHAccess")).SelectedItem.Value;

Then call ur Update Procedure to update in database.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 9:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC