views:

362

answers:

2

hi

I want to delete rows in my gridview whit button delete on keyboard.what do I do?

+2  A: 

try this

add a delegate to your gridView like this in the form.designer.cs file. Find the area where your gridview properties are being set and add this line at the bottom of the section.

this.gridview.KeyDown += new System.Windows.Forms.KeyEventHandler(gridview_KeyDown);

Then in your event handler do the following

void gridview_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        //checks to see if the delete button has been clicked and if there are selected rows
        if (ModifierKeys == Keys.Delete && gridview.SelectedRows.Count > 0)
        { 
                //perform delete
        }
    }
gsirianni
my friend my proble now is the row isn't delete when I press delete key and I know my code isn't correct.BECAUSE I don't know what I compare two value?
mohammad reza
A: 

I write this code and delete my selected row .

        private void GridviewName_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
            {
               var delete = from del in Movie_List.Requests
                            where del.Movie_Name == GridviewName.Rows[GridviewName.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString()
                             select del;
                foreach (var Item in delete)
                {
                    Movie_List.Requests.DeleteOnSubmit(Item);
                    Movie_List.SubmitChanges();
                }
            }

Good Luck

mohammad reza