Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a GridView control in my page, each row has a button called "Split",
On Click of that button that row should get copied in same gridview.

Please give me code , how to handle it out.

Thanks & Regards,
Harshada
Posted

On click event of Split button take Identity for that row means Primary key of that record which is on that table by this value you can get all value of that row then insert it again in table this way you can add duplicate record.
If not solve past you code how you do this we can help.
 
Share this answer
 
Comments
jaideepsinh 3-Jul-13 6:42am    
You want to add record only in grid view not want to insert record in database.
Hi Harshada

onclick event of Button you can find primery key of the table like

int ID = Convert.ToInt32(Grid_ShowData.DataKeys[e.NewEditIndex]["ID"].ToString())

where ID is the primery key of table
then
insert the all data into table of that id, and Bind table table to gridview.

If you paste your code i give you the solution
 
Share this answer
 
v2
Comments
Member 8273740 3-Jul-13 4:01am    
Before doing split i have two rows already in my datatable & also in gridview.

protected void gvGRNItems_RowCommand(object sender, GridViewCommandEventArgs e)
{if (e.CommandName == "Split")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).Parent.BindingContainer;
if (UpdateGRNItemSplit(Convert.ToInt32(e.CommandArgument), gr))
{
//gvGRNItems.EditIndex = -1;
FillGRNItems();
}
}
}

protected bool UpdateGRNItemSplit(int p, GridViewRow gr)
{
bool bolRetVal = false;

Label lblPOSchID = (Label)gr.FindControl("lblScheduleNo");

DataRow drNew;
drNew = dtGRNItem.NewRow();
DataView dv;
dv = dtGRNItem.DefaultView;
dv.RowFilter = "POScheduleId=" + Convert.ToInt32(lblPOSchID.Text);
DataTable dt = new DataTable();
if (dv.ToTable().Rows.Count > 0)
{
dt = dv.ToTable();
}

foreach(DataRow dr in dt.Rows)
{
drNew["GRNItemID"] = CurrentGRNItemId--;
drNew["GRNID"] = iGRNID;
drNew["Sno"] = dtGRNItem.Rows.Count + 1;
drNew["ItemId"] = dr["ItemId"];
drNew["POID"] = dr["POID"];
drNew["POScheduleId"] = dr["POScheduleId"];
drNew["PONO"] = dr["PONO"];
drNew["PODATE"] = dr["PODATE"];
drNew["Date"] = dr["Date"];
drNew["Qty"] = dr["Qty"];
drNew["OurName"] = dr["OurName"];
dtGRNItem.Rows.Add(drNew);
}


dtGRNItem.AcceptChanges();

bolRetVal = true;

return bolRetVal;
}

private void FillGRNItems()
{
gvGRNItems.ShowFooter = (gvGRNItems.Enabled == true && dtGRNItem.Rows.Count > 0 && gvGRNItems.EditIndex < 0 && txtChallanNo.Enabled);
gvGRNItems.DataSource = dtGRNItem;
gvGRNItems.DataBind();
}

Everything till here works fine...but when i bind the datatable to gridview ,only two rows gets binded , the third row doesnt get binded to gridview.
jaideepsinh 3-Jul-13 5:41am    
You want to record only in grid view not want to insert record in database?
Member 8273740 3-Jul-13 8:47am    
Thanks EveryOne...I have solved it myself....the problem was with DataView
Member 11202913 19-Aug-15 13:02pm    
i know its been a while since you solved this issue, but i am having the same issue could you please send me your updated code to aranganathan55@gmail.com. i am trying to create a duplicate and then have that row in edit mode for users.
Member 14932854 15-Mar-21 7:10am    
Hi, I am also facing same issue. Please share your updated code.
My email is christo.oosthuizen@scaw.co.za

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