Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I take a data table to bind grid view.
It after clicking on add button it inserted data into grid view successfully.
I take link button in grid view to edit and delete.
on click on delete it deleted data of selected row.
On click on select(i.e edit) it assign a value to drop down list and text boxes successfully.
after clicking on edit and changing values on can again click on add button .At this time I want to Update the selected row with new values,
I cannot able to solve this problem
Can any one help me please.
Thanks in advance.

My code is as follow
<asp:GridView ID="grdvPart" runat="server" AllowPaging="True" AllowSorting="True" CssClass="table table-bordered"
                            AutoGenerateColumns="False" CellPadding="0"  GridLines="None"  Width="100%" ShowFooter="True" OnRowCommand="grdvPart_RowCommand" >
          <Columns>
          
                 <asp:BoundField DataField = "prt_id" HeaderText = "Shape Part code" />
                 <asp:BoundField DataField = "prt_nm" HeaderText = "Parts" />
                 <asp:BoundField DataField = "shpprtQuantity" HeaderText = "Quantity" />
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton  ID="lbtnSelect" runat="server" Text = "Select" CommandArgument='<%# Eval("prt_id")%>' CommandName="SelectRecord"></asp:LinkButton>
                         </ItemTemplate>
                   </asp:TemplateField>    
                   <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton ID="lblremove" runat="server" Text = "remove" CommandArgument='<%# Eval("prt_id")%>' CommandName="DeleteRecord"></asp:LinkButton>
                    
                        </ItemTemplate>
                        
                </asp:TemplateField>               
               </Columns>
         </asp:GridView>


Code Behind
on page load{
DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("prt_id"), new DataColumn("prt_nm"), new DataColumn("shpprtQuantity") });
                ViewState["Part"] = dt;
this.bindDetails();
}

  private void bindDetails()
        {
            grdvPart.DataSource = (DataTable)ViewState["Part"];
            grdvPart.DataBind();
}

protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt;
            dt = (DataTable)ViewState["Part"];
 dt.Rows.Add(tbxPCode.Text, ddlPart.SelectedItem.Text, tbxQuantity.Text);
  ViewState["Part"] = dt;
this.bindDetails();
            clear1();
        }
   protected void grdvPart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
if (e.CommandName.Equals("DeleteRecord"))
            {
 DataTable dt = (DataTable)ViewState["Part"];
if (dt.Rows.Count >= 0)
                        {

                            dt.Rows.RemoveAt(Convert.ToInt16(grdvPart.SelectedRow));
                            this.bindDetails();
                        }
}
  if (e.CommandName.Equals("SelectRecord"))
            {
              
                    string code = grdvPart.Rows[Convert.ToInt32(grdvPart.SelectedRow)].Cells[0].Text;
                    tbxPCode.Text = code;
                    ddlPart.SelectedItem.Value = code;
                    ddlPart.SelectedItem.Text = grdvPart.Rows[Convert.ToInt32(grdvPart.SelectedRow)].Cells[1].Text;
                    HiddenFieldpart.Value = id.ToString();
}
Posted
Updated 17-Nov-15 22:34pm
v2

1 solution

You are doing all operations on cached data. Send that data table to database access method and loop through the rows. Important thing is that you DO NOT call AcceptChanges before saving to the database or states will be removed. Then you would have to go row by row and check each and every one and that is more pain then it is worth.

C#
foreach (DataRow r in myTable.Rows) {
    switch (r.RowState) {
        case RowState.Added
 // insert this one
        case RowState.Modified
// update this one
        case RowState.Deleted
// delete this one
        case RowState.Dettached
// probably delete too
    }
}
 
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