所以我有一個名為 MyMovieDataDrid 的 DataGridView,它連接到一個 sql 服務器,我已經有我的 IsDeleted 屬性和一個如下所示的洗掉存盤庫,順便說一下,我的 IsDeleted 屬性自動顯示為一個復選框,所以如果選中了復選框這是真的,如果不是它是假的
public bool DeleteMovie(Guid id)
{
bool isDeleted = false;
var movie = _context.Movie.FirstOrDefault(m => m.Id == id);
if (movie != null)
{
movie.IsDeleted = true;
_context.SaveChanges();
isDeleted = true;
}
return isDeleted;
}
這是我的洗掉按鈕方法,所以當我按下它時,它會運行我的邏輯并從 DataGridView 軟洗掉一行我嘗試了多種解決方案,例如使用選定的行事件處理程式來獲取選定的行,然后運行存盤庫方法,但沒有到目前為止已經作業了。
private void DeleteButton_Click(object sender, EventArgs e)
{
Movie movie = new Movie();
if(MyMovieDataGrid.SelectedRow.Count > 0)
{
_movieRepo.DeleteMovie(movie.Id);
}
}
和我所有的財產
Movie movie = new Movie()
{
Id = Guid.NewGuid(),
IsDeleted = false,
MovieNames = MovieNameBox.Text;
}
和我的 AddMovie 存盤庫
public void AddMovie(Movie movie)
{
_context.Movie.Add(movie);
_context.SaveChanges();
}
電影存盤方法
private NRIDataContext _context;
public MovieRepository()
{
_context = new NRIDataContext();
}
//GetMovie repository
GetMovie()
{
var movies = _context.Movie.Where(m => m.IsDeleted
==false).ToList();
return movie;
}
MyMovieDataGrid.DataSource = _movieRepo.GetMovie().OrderByDescending(x => x.MovieNames.First) .ToList();
所以我的問題是如何讓我的 Datagrid 知道何時運行我的存盤庫方法,我覺得我必須以某種方式撰寫一些代碼,如果 IsDeleted 屬性為真,它會選擇整行然后我運行我的 DeleteMovie 方法但沒有解決方案已經奏效。
uj5u.com熱心網友回復:
我喜歡 Karen 的方法,因為它更像是“應該如何完成”,但這與您撰寫的內容相去甚遠,我懷疑您可能不想大規模更改當前代碼
您的方法的基本問題是您沒有從單擊的行中獲取電影 ID,您制作了一部新電影,該電影具有一個新的隨機 Guid,據說它根本不會在資料庫中,因為 Guid 不太可能重復,然后你嘗試洗掉它:
private void DeleteButton_Click(object sender, EventArgs e)
{
//make a new movie with random id
Movie movie = new Movie();
if(MyMovieDataGrid)
{
//delete the new random id
_movieRepo.DeleteMovie(movie.Id);
}
}
這意味著您要洗掉的電影實際被洗掉的幾率是驚人的 5316911983139663491615228241121400000 比 1;如果您現在開始點擊并繼續前進,直到地球被太陽吞噬紅巨星,這可能不會發生??
從單擊的行中檢索要洗掉的 Guid;添加一個 CellContentClicked 處理程式并檢查它是被單擊的已洗掉按鈕列,然后將電影 id 從行的資料系結項中拉出:
private void DGV_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
//don't process clicks on the header row
if(e.RowIndex < 0) return;
//don't process clicks on columns other than delete
if(e.ColumnIndex != MyMovieDataGrid.Columns["nameOfYourDeleteColumn"].ColumnIndex) return;
//pull the movie from that row
Movie movie = MyMovieDataGrid.Rows[e.RowIndex].DataBoundItem as Movie;
if(MyMovieDataGrid) //I don't know what this means; a DataGridView is not a boolean
{
//delete the movie id
_movieRepo.DeleteMovie(movie.Id);
}
}
uj5u.com熱心網友回復:
考慮在這種情況下設定一個過濾器,因為我已經OnModelCreating使用了一個名為的模型Contact1。在基礎表中添加IsDeleted列,但不在模型中。
下面顯示了軟洗掉行并將其從 DataGridView 中洗掉的基礎知識。沒有適當的存盤庫。
public partial class Contact1 : INotifyPropertyChanged
{
private int _contactId;
private string _firstName;
private string _lastName;
public int ContactId
{
get => _contactId;
set
{
_contactId = value;
OnPropertyChanged();
}
}
public string FirstName
{
get => _firstName;
set
{
_firstName = value;
OnPropertyChanged();
}
}
public string LastName
{
get => _lastName;
set
{
_lastName = value;
OnPropertyChanged();
}
}
public Contact1()
{
}
public override string ToString() => $"{FirstName} {LastName}";
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
OnModelCreating
modelBuilder.Entity<Contact1>()
.HasQueryFilter(contact =>
EF.Property<bool>(contact, "isDeleted") == false);
然后override SaveChanges在 DbContext
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new ())
{
DoShadowyStuff();
return base.SaveChangesAsync(cancellationToken);
}
public override int SaveChanges()
{
DoShadowyStuff();
return base.SaveChanges();
}
然后DoShadowlyStuff如果狀態為已洗掉,則將狀態設定為已修改。
private void DoShadowyStuff()
{
ChangeTracker.DetectChanges();
foreach (var entry in ChangeTracker.Entries())
{
if (entry.State == EntityState.Deleted)
{
// Change state to modified and set delete flag
entry.State = EntityState.Modified;
entry.Property("isDeleted").CurrentValue = true;
}
}
}
非常基礎read和delete操作
public class Operations
{
public static void Remove(Contact1 contact1)
{
using (var context = new ShadowContext())
{
context.Add(contact1).State = EntityState.Deleted;
context.SaveChanges();
}
}
public static List<Contact1> Contacts()
{
using (var context = new ShadowContext())
{
return context.Contacts1.ToList();
}
}
}
基本的表單操作。單擊洗掉按鈕,將當前行的狀態設定為已洗掉,保存將聯系人標記為已修改的更改并設定 isDeleted。接下來從 BindingList 中洗掉洗掉,然后從 DataGridView 中洗掉該行。
public partial class Form1 : Form
{
private BindingList<Contact1> _bindingList;
private readonly BindingSource _bindingSource = new ();
public Form1()
{
InitializeComponent();
Shown = OnShown;
}
private void OnShown(object sender, EventArgs e)
{
_bindingList = new BindingList<Contact1>(Operations.Contacts());
_bindingSource.DataSource = _bindingList;
dataGridView1.DataSource = _bindingSource;
}
private void DeleteButton_Click(object sender, EventArgs e)
{
Operations.Remove(_bindingList[_bindingSource.Position]);
_bindingList.RemoveAt(_bindingSource.Position);
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/478045.html
