•
•
•
•
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
![]() |
| |
•
•
Join Date: Aug 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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 !!!!!!!!!!!!!!!!!
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 !!!!!!!!!!!!!!!!!
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:
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!
//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.
•
•
Join Date: Aug 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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
•
•
Join Date: Aug 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks Edek, It worked for me...
•
•
•
•
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
•
•
Join Date: Feb 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 1
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
http://fragilegirl-devblog.blogspot....atagrid-c.html
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 .......
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.
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How to prevent moving to any row of the datagrid if there is error while editing. (VB.NET)
- Adding row in datagrid (ASP.NET)
- How to delete a particular row from a datagrid..? (VB.NET)
- select row in datagrid....need help (VB.NET)
- Having problems with adding a row to a DataSet (VB.NET)
- Deleting a Row From A DataGrid (VB.NET)
Other Threads in the C# Forum
- Previous Thread: Number of query values and destination fields are not the same
- Next Thread: Searching Between Dates


Hybrid Mode