Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void btnAddServices_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
DataColumn dc;

dc = new DataColumn("Description");
dt.Columns.Add(dc);
dc = new DataColumn("Duration");
dt.Columns.Add(dc);
dc = new DataColumn("Price");
dt.Columns.Add(dc);
foreach (DataGridViewRow row in gvBillGenerate.Rows)
{
dr = dt.NewRow();

dr["Description"] = cmbService.SelectedValue;
dr["Duration"] = txtDuration.Text;
dr["Price"] = txtPrice.Text;

dt.Rows.Add(dr);

}
dt.AcceptChanges();
gvBillGenerate.DataSource = dt;


it will display multiple rows with same data.
Posted

foreach (DataGridViewRow row in gvBillGenerate.Rows)
{
dr = dt.NewRow();

dr["Description"] = cmbService.SelectedValue;
dr["Duration"] = txtDuration.Text;
dr["Price"] = txtPrice.Text;

dt.Rows.Add(dr);

}
dt.AcceptChanges();


you are passing same value.Because only one value can exit on textbox and dropdown at a time.
If you are going to add rows dynamically you should store previous data in ViewState or Session whatever your need is and take data From there and Add new rows.

ViewState["Bill"] = dt;
dt=ViewState["Bill"] as DataTable
 
Share this answer
 
v3
Comments
nainika_ 28-Oct-14 0:46am    
but i am using windows applications , i can't use viewstate over there.
Laiju k 28-Oct-14 0:50am    
remove the foreach part maintain with same datatable no need to initializing it every time I dont Know about Windows.try it


[no name] 28-Oct-14 0:55am    
yeah Laiju is right. I agree.
nainika_ 28-Oct-14 0:59am    
even i can't use sessions in windows applications, i ll only be possible in asp.net.
Laiju k 28-Oct-14 1:00am    
I have changed the comment you try that
Hi Nainika,

Check these values:
C#
dr["Description"] = cmbService.SelectedValue;
dr["Duration"] = txtDuration.Text;
dr["Price"] = txtPrice.Text;


cmbService will have only one selected value. Check what text you have in these two TextBoxes. I guess the values that you are assigning will remain the same.

Apply a debugger and check them first.

Regards,
Praneet
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900