Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have gridview which i populated with datatable that in not filled by database records rather it is filled by textbox value on button click. Now what I want is, I want to delete the selected row of gridview. (No connection to database records).

"Thanking You"
Posted
Updated 24-Jan-17 17:52pm

I think you can use this code to remove a selected row from your data grid view.

C#
if (dataGridView1.SelectedRows.Count > 0)
    dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
 
Share this answer
 
Hi ,
you can use also this one

C#
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
      dt.Rows.RemoveAt(e.NewSelectedIndex);
      GridView1.DataSource = dt;
      GridView1.DataBind();
  }
 
Share this answer
 
Comments
Gopal Rakhal 29-Mar-12 13:20pm    
I got u, thanks but as I am fist time working on Web application, don't have through idea please help little more, "Thanking You"
Mohamed Mitwalli 29-Mar-12 19:03pm    
you want to delete from Gridview and not from the DB
the First line dt.Rows.RemoveAt(e.NewSelectedIndex); iam specify the index for the row in Datatable so i can remove it .
GridView1.DataSource = dt;
GridView1.DataBind();
and rebind the datatable again .
if you need help let me know and i will help :)
Best Regards
M.Mitwalli
Mohamed Mitwalli 29-Mar-12 19:06pm    
by the Way if this solution help you mark it so any one else can make benefit .:)
if (dataGridView1.SelectedRows.Count > 0)
                   {
                       dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
                   }
 
Share this answer
 
bind the data from database to a datatable like "dbTable". Use dbTable to bind the gridview.

on gridview_rowDeleting ...write following code...

C#
dbTable.Rows[e.RowIndex].Delete();
Gridview.DataSource =dbTable;
GridView.DataBind();
 
Share this answer
 
v2
Comments
Pragasam.s 11-Aug-17 6:04am    
what is e.RowIndex meaning here

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