Hello Everyone, Welcome to my blog for Sharing and Learning together! I hope you have a nice day! :)

Delete Select Gridview on C# Windows Forms with Entity Framework

+ No comment yet
hai kawan, karena hari ini aku lagi belajar coding c# cara menggunakan gridview lebih khususnya function delete jadi ku posting codenya aja, semoga semuanya paham.

private void DeleteItemCategory()
        {

            List rowsToDelete = new List();
            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                if (rowsToDelete.Contains(cell.RowIndex) == false)
                    rowsToDelete.Add(cell.RowIndex);
            }
            rowsToDelete = rowsToDelete.OrderByDescending(rowIndex => rowIndex).ToList();

            foreach (Int32 rowIndex in rowsToDelete)
            {
                if (dataGridView1.Rows[rowIndex].Cells[0].Value != null)
                {
                    a = _context.ItemCategories.Find(dataGridView1.Rows[rowIndex].Cells[0].Value);
                }
                dataGridView1.Rows.RemoveAt(rowIndex);
            }
            _context.Entry(a).State = EntityState.Deleted;
            rowsToDelete.Clear();
            _context.SaveChanges();
  }   

maaf ya masih belum rapi, tapi tenang aja esok kan ku update lagi... :) Have a nice coding!

Post a Comment