我有daragrid,我想用按钮检测选定的行。我该怎么做?感谢
dataGrid.ItemsSource = new List<User>(FormOfAddData.MyList);
答案 0 :(得分:2)
将您的数据绑定到可观察的集合。只需将其从您的收藏中删除即可。
var myData = new ObservableCollection<User>(FormOfAddData.MyList);
dataGrid.ItemsSource = myData;
private void OnButtonClick(){
User user = dataGrid.SelectedItem as User;
if(user != null){
myData.Remove(user);
}
}
答案 1 :(得分:0)
if (datagrid1.SelectedItem != null)
{
datagrid1.Items.Remove((BindingData)datagrid1.SelectedItem);
}
答案 2 :(得分:0)
如果您使用ObservableCollection
而不是{@ 1}} @Tobias Theel建议:
List
..您可以按如下方式从源集合中删除所选项目。
<强> XAML:强>
dataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<User>(FormOfAddData.MyList);
<强>代码:强>
<Button Content="Delete" Click="Button_Click_1" />
如果您继续使用private void Button_Click_1(object sender, RoutedEventArgs e)
{
User user = dataGrid.SelectedItem as User;
if (user != null)
{
IList<User> users = dataGrid.ItemsSource as IList<User>;
if (users != null)
users.Remove(user);
}
}
,则必须重置List<User>
属性:
ItemsSource
答案 3 :(得分:-1)
您可以使用以下内容;
private void DeleteSelectedRows_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGrid.SelectedRows)
{
dataGrid.Rows.RemoveAt(row.Index);
}
}