我遇到以下問題。WPF DG-如何刪除DataGrid上Button的單擊事件
我從我的DataGrid中用鼠標點擊選擇一行,如下所示: Admin admin =(Admin)dGrid.SelectedItem;
如何使用按鈕單擊事件刪除此行?我無法使用Rows.Delete,也沒有發現任何刪除,刪除WPF中的命令。謝謝你的幫助!
我遇到以下問題。WPF DG-如何刪除DataGrid上Button的單擊事件
我從我的DataGrid中用鼠標點擊選擇一行,如下所示: Admin admin =(Admin)dGrid.SelectedItem;
如何使用按鈕單擊事件刪除此行?我無法使用Rows.Delete,也沒有發現任何刪除,刪除WPF中的命令。謝謝你的幫助!
如果您將DataGrid綁定到可觀察集合(以兩種方式),那麼從集合中刪除選定的項目就是您需要完成的工作; 這裏是如何進行(的DoubleClick要刪除)的示例:
<Grid>
<DataGrid ItemsSource="{Binding AdminCollection,Mode=TwoWay}" SelectedItem="{Binding SelectedAdmin}" MouseDoubleClick="DeleteRowEvent" >
</DataGrid>
</Grid>
和後面的相應的代碼:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Admin _selectedAdmin = new Admin();
public Admin SelectedAdmin
{
get { return _selectedAdmin; }
set
{
if (_selectedAdmin == value) return;
_selectedAdmin = value;
OnPropertyChanged();
}
}
private ObservableCollection<Admin> _adminCollection = new ObservableCollection<Admin>();
public ObservableCollection<Admin> AdminCollection
{
get { return _adminCollection; }
set
{
if (_adminCollection == value) return;
_adminCollection = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
AdminCollection = new ObservableCollection<Admin>()
{
new Admin()
{
Name = "James",Age = 34, Location = "Paris"
},
new Admin()
{
Name = "Joe",Age = 34, Location = "Us"
},
new Admin()
{
Name = "Ann",Age = 34, Location = "Canada"
},
};
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private void DeleteRowEvent(object sender, MouseButtonEventArgs e)
{
AdminCollection.Remove(AdminCollection.First(x => x.Id == SelectedAdmin.Id));
}
}
public class Admin
{
public string Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public int Age { get; set; }
}
在DataContext設置爲後面的代碼在MainWindow.xaml
DataContext="{Binding RelativeSource={RelativeSource Self}}"
這實際上取決於你如何將數據綁定到DataGrid。
由於它看起來像綁定了一個可管理類型的枚舉,因此應該在列表中找到它並將其刪除。然後DataGrid應該更新。
如果u手動創建DataGrid和使用LINQ然後ü可以嘗試
這樣的: -
<Window x:Class="Wpf_grid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="326" Width="946">
<Grid>
<DataGrid Name="MyDataGrid" Uid="MyDataGrid" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,31,0,0" IsReadOnly="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=RegId}" IsReadOnly="False" Header="Registration Id" Width="sizeToHeader"/>
<DataGridTextColumn Binding="{Binding Path=Name}" IsReadOnly="False" Header="Name" Width="sizeToHeader" />
<--Many column here-->
<DataGridTemplateColumn Header="Edit Row">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit" Click="btnEdit_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Delete Row">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Click="btnDelete_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
這裏是的.cs代碼
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
gridDataContext gcdd = new gridDataContext();
registration RegistrationRow = MyDataGrid.SelectedItem as registration;
var Registration = (from p in gcdd.registrations where p.RegId == RegistrationRow.RegId select p).Single();
gcdd.registrations.DeleteOnSubmit(Registration);
gcdd.SubmitChanges();
MessageBox.Show("Row Deleted Successfully");
LoadCustomerDetail();
}
謝謝您的解答!約瑟夫,我試過你的代碼,我沒有得到任何數據到我的DG。我知道什麼是問題,但我不知道如何解決它。因此,您在AdminCollection = new ObservableCollection() {new Admin() {...}}下設置新值;我理解你的代碼,但我不知道應該如何替換...部分,因爲我早前通過使用LINQ的SQLQuery爲我的所有字段賦予了值。那麼,如果您已將值賦予所有字段,那麼您在新的Admin()下做了什麼?試過AdminCollection = new ObservableCollection ();也。 –
Tom026
2014-11-02 17:01:06
只是使用從linq查詢返回的列表instanciate Observable集合:AdminCollection = new ObservableCollection((linq query).toList) –
Usama
2014-11-02 20:54:10
好吧,我做了你所說的,現在這一部分是確定的。但我沒有收到任何數據,所以我使用了一些斷點。 AdminCollection是問題所在,當程序運行時使用遊標移動它時,count = 0(selectedadmin正常,它知道表中有多少行,其餘的代碼將數據源返回給數據源當我用斷點運行它)。其餘的找到桌子。如果我能做到這一點,我會接受你的答案作爲解決方案! :)如果您有任何想法,請與我分享。我真的很感謝你的幫助約瑟夫! – Tom026 2014-11-03 05:34:42