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 !!!!!!!!!!!!!!!!!

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!

I would also use Table.AcceptChanges(); after the row was added...

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

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

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.com/2008/02/adding-new-record-inside-datagrid-c.html

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.

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 .......

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.

Using that first poster's code, I get System.Data.DataRow.DataRow(System.Data.DataRowBuilder) is inaccessible due to its protection level (regardless of if I run the code within "protected void Page_Load(object sender, EventArgs e)" or return a DataTable from another function back into Page_Load so it can use it for my DataGrid) and anywhere someone has posted something about DataGrid1.CurrentRowIndex - when using that I get System.Web.UI.WebControls.DataGrid does not contain a definition for CurrentRowIndex. I'm using the DataGrid in a ASP .NET AJAX-enabled site, using .NET 2.0 in Visual Studio 2005.

I found out you have to first create DataColumns and make their ReadOnly property equal to false (dc = new DataColumn(); dc.ReadOnly = false; ). You specify a dc.DataType and dc.ColumnName and then add the "dc" to a datatable (dt.Columns.Add(dc)). Then you can populate the row's columns (dr["myColumnName"] = someData) and add the row(s) to the DataTable (dt.Rows.Add(dr)). You can then set DataGrid1.DataSource = dt and do DataGrid1.DataBind() and it works. You do NOT need to go through a DataSet, and that would be extra work - even on a database query, since you can cast an ExecuteScalar as a DataTable and then fill the GridView directly ( DataGrid1.DataSource = dt; DataGrid1.DataBind() ). See http://www.csharphelp.com/2005/12/introduction-to-datatables-part-i/.

-Tom

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.