thevan
thevan

Reputation: 10364

How to delete the newly added Rows in Gridview?

I have two GridViews. One is Source GridView and another one is Destination GridView. Two GridViews are loaded with some records. In the Destination GridView, One CommandField Column is added which shows "Delete". Here I want to move one row from the Source GridView to the Destination GridView. Suppose if i dont want, I can remove the newly added record by clicking the Delete Command Field in the GridView.

Except the Deleting the Rows, I have completed the other things. How to delete the newly added records. The DeleteCommand Field in the Destination Gridview is enabled only for the newly added records otherwise it should enabled false. Any Suggestions.

Upvotes: 0

Views: 750

Answers (3)

vikas mehta
vikas mehta

Reputation: 458

Do not add commandfield column rather add a templatefield. in that templatefield add a itemtemplate, an ImageButton and keep the enabled property of ImageButton as true for rows which are added from source grid and false for others.(This you can check by keeping a label in gridview which should be hidden and set its text to "true" for rows got from source grid) EXAMPLE :

   <asp:TemplateField HeaderText="Delete">
 <ItemTemplate>
 <asp:ImageButton ID="imgbtnDelete" ToolTip="Delete" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"SomeField") %>' CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>

in CodeBehind

  protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
  Label lbl = (Label)e.Row.FindControl("lbl");
  ImageButton imgbtnDeleteUser = (ImageButton)e.Row.FindControl("imgbtnDelete");
  if (imgbtnDeleteUser != null && lbl.Text==true)
  {
        imgbtnDeleteUser.Enabled = true;    
  }
  else
  {
        imgbtnDeleteUser.Enabled = false;    
  }
}

Upvotes: 1

Jude Cooray
Jude Cooray

Reputation: 19862

If you are using two data tables for the different grid views, perhaps you can use the DataRow's DataRowState enumeration.

Have the delete button in a TemplateField

Upvotes: 1

FIre Panda
FIre Panda

Reputation: 6637

Assuming that you render delete button for each DataGridView row, in the delete button tag property, you could save the index of the row it is on, on the event handler of the delete button, you could get the row index and then you could delete the row from datagridview. This the case when you want to delete the record from the database as well or you want to do it at server side.

But if you are adding the rows at clientside, you could remove the rows from the datagridview using javascript, as datagridview is rendered as html table and you could remove the rows from html table using javascript.

Upvotes: 1

Related Questions