User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 391,762 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,201 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 ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 6726 | Replies: 6
Reply
Join Date: Nov 2007
Posts: 18
Reputation: Nareshp_123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Nareshp_123 Nareshp_123 is offline Offline
Newbie Poster

Help Dynamically add rows in datagrid

  #1  
Dec 5th, 2007
Hi
I need to make application similar to excel sheet .like i have 4 columns where in (1 row will be static to display header)
and below 1 column will have a dropdownlist and remaining 3 columns are textboxes

user should be able to add as many rows as possible dynamically .

for ex:-if i have static label as below
ID Name Age Address

then ID will be dropdownlist,
Name,Age,Address--are textboxes .
user can add as many rows as possible.anybody please help me in this......

Naresh
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Dynamically add rows in datagrid

  #2  
Dec 7th, 2007
You can put the controls you need in the footer row of your table:

http://aspnet.4guysfromrolla.com/articles/021203-1.aspx
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Nov 2007
Posts: 18
Reputation: Nareshp_123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Nareshp_123 Nareshp_123 is offline Offline
Newbie Poster

Help Re: Dynamically add rows in datagrid

  #3  
Dec 8th, 2007
Originally Posted by alc6379 View Post
You can put the controls you need in the footer row of your table:

http://aspnet.4guysfromrolla.com/articles/021203-1.aspx


hi Thanks for your reply .it seems i had not given the complete information.
in the example what you gave shows the data from sql database. and add's data to it.

but for me
1.i need to show empty row(which is 1 row). when 1 row is filled and when he press Add button.
2 Row with empty filed should be displayed.---like this it goes upto 'n rows'... this process is dynamical where later on based on number of rows(say 'n') ..i need to create ('n') number of tags in xml file (its in the middle of xml) after creation of tags i insert these data into those tags.

i hope you understood pls help me in this.
also let me know which is the right one i am new to asp.net. what i checked from net shows all example are like getting,insert,delete data from sql.

1.is that i need to create datatable and bind controls to it dynamicallly on button click?
2.is that need to be done through datagrid ?
or through any other way pls let me know.

Regards
Naresh
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Dynamically add rows in datagrid

  #4  
Dec 9th, 2007
This will still do it-- that's why you put the controls in the footer row of the datagrid. That way, there will always be a new row. You could even go so far as to make an Add button that, when pressed, can display those fields.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Nov 2007
Posts: 18
Reputation: Nareshp_123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Nareshp_123 Nareshp_123 is offline Offline
Newbie Poster

Solution Re: Dynamically add rows in datagrid

  #5  
Dec 10th, 2007
Originally Posted by alc6379 View Post
This will still do it-- that's why you put the controls in the footer row of the datagrid. That way, there will always be a new row. You could even go so far as to make an Add button that, when pressed, can display those fields.


hi thank you for your reply.
i did it with the following example.
http://www.sitepoint.com/article/net...t-datatables/2
this is what have done:-
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
Category: &nbsp;
Name: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
UoM:<br> <br>
<aspropDownList id="ddlProducts" runat="server">
<asp:ListItem>Serial Information</asp:ListItem>
<asp:ListItem>Product Ratings</asp:ListItem>
<asp:ListItem>Commercial Information</asp:ListItem>
<asp:ListItem>Rating Plate</asp:ListItem>
</aspropDownList>
<asp:textbox id="txtName" runat="server" />
<asp:textbox id="TxtValue" runat="server" />
<asp:textbox id="TxtMetrics" runat="server" /><br> <br>
<asp:Button id="btnAdd" runat="server" Text="Add new Product Property"
onClick="AddToCart" /><br>
<br>
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True"
Height="76px" Width="694px" AllowSorting="True" AutoGenerateEditButton="True" OnRowDeleting="GridView1_RowDeleting">
</asp:GridView>
<br>
<br> <br> <br><br>
<div>
&nbsp;</div>
</form>
</body>
</html>
----------------------------
public partial class _Default : System.Web.UI.Page
{
DataTable objDt;
DataRow objDr;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddProductData();
}

}
public void AddProductData()
{
objDt = new DataTable("ProductData");
objDt.Columns.Add("Category");
objDt.Columns.Add("Name");
objDt.Columns.Add("Value");
objDt.Columns.Add("UoM");
Session["Product"] = objDt;
}
protected void AddToCart(object sender, EventArgs e)
{
objDt =(DataTable) Session["Product"];
string Category = ddlProducts.SelectedItem.Text;

objDr = objDt.NewRow();
objDr["Category"] = ddlProducts.SelectedItem.Text;
objDr["Name"] = txtName.Text;
objDr["Value"] = TxtValue.Text;
objDr["UoM"] = TxtMetrics.Text;

objDt.Rows.Add(objDr);
Session["Product"] = objDt;

GridView1.DataSource = objDt;
GridView1.DataBind();


}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{


objDt = (DataTable)Session["Product"];
objDr = objDt.Rows[e.RowIndex];
objDr.Delete();

Session["Product"] = objDt;
GridView1.DataSource = objDt;
GridView1.DataBind();

}



}


is that fine i dont think using session is the best solution.
if you suggest any advice pls reply to this posting so that i can mark this post as solved.

Naresh
Last edited by Nareshp_123 : Dec 10th, 2007 at 4:34 am.
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Dynamically add rows in datagrid

  #6  
Dec 10th, 2007
Yeah... session doesn't really seem like such a great idea. Where is that objDt getting populated from to begin with?

I mean... unless you're saving the state in a database or something. That's the only way it would make sense to me. Perhaps that's why they're using state?
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Nov 2007
Posts: 18
Reputation: Nareshp_123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Nareshp_123 Nareshp_123 is offline Offline
Newbie Poster

Re: Dynamically add rows in datagrid

  #7  
Dec 10th, 2007
Originally Posted by alc6379 View Post
Yeah... session doesn't really seem like such a great idea. Where is that objDt getting populated from to begin with?

I mean... unless you're saving the state in a database or something. That's the only way it would make sense to me. Perhaps that's why they're using state?


Hi objDT is datatable object
here goes the entire scenario....
if page is not postback,then i am creating a datatable with the required columns(1 dropdown,3 textboxes->4column)
and i storing it in session.-> then binding it to gridview
once the user enters the values into textboxes,dropdownlist. and click on ADD button
i am getting the stored datatable,and creating a new row with these valuesin the datatable and again passing back datatable to session object. and binding the updated datatable to gridview.


since here i dont have database to store.that is the problem for me.so,i am using datatable to store the value from user input.
then i getting the datatable for later purpose(for inserting values into xml file based on number of rows ,i am getting the count and creating 'count' number of tags in xml file and inserting these values into xml.....

everything works fine even delete function in gridview.
but i am not able to implement that functionality of edit in gridview
#region GridView RowEdit Event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//get datatable from session object & get the row to be edited
objDt = (DataTable)Session["Product"];
objDr = objDt.Rows[e.NewEditIndex];
////
////funtionality to edita row in gridview.
////
//Bind the datatable to gridview
GridView1.DataSource = objDt;
GridView1.DataBind();
}
it's ok since as of now this functionality is not major .if it is needed then i concentrate on this..

I hope you understood the entire scenario.if you suggest any suggestion pls let me know ......
since i want to make it stable....so that in future we should not face any problem...

thanks for your help..

Naresh
#endregion
Last edited by Nareshp_123 : Dec 10th, 2007 at 11:50 pm.
Reply With Quote  
Reply

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

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

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