WPF DataGrid简介使用
2013-12-18 10:41
369 查看
(1)自动生成列
<DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick" />
(2)取消自动生成列,手动绑定到相应字段
<DataGrid AutoGenerateColumns="False" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="Info" Binding="{Binding Info}" Width ="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
最后一列设置Width ="*"是为了取消空白列。
(3)后台代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = AccessDAL.OleDbHelper.ExecuteDataTable("SELECT * from 表名").DefaultView;
}
//双击DataGrid,显示相应信息
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = datagrid.SelectedItem as DataRowView;
MessageBox.Show(row["id"].ToString());
}
//如果绑定到对象集合,如ObservableCollection<Employee>,代码如下:
ObservableCollection<Employee> col;
public EmployeeManage()
{
InitializeComponent();
col = new ObservableCollection<Employee>();
col.Add(new Employee() { Id = 1, Name = "张三", Info = ********* });
col.Add(new Employee() { Id = 2, Name = "李四", Info = ********* });
datagrid.ItemsSource = col;
}
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Employee emp=datagrid.SelectedItem as Employee;
MessageBox.Show(emp.Id.ToString());
}
(4)删除选中的多行数据
private void Delete_Click(object sender, RoutedEventArgs e)
{
for (int i = datagrid.SelectedItems.Count - 1; i >= 0; i--)
{
Good good = datagrid.SelectedItems[i] as Good;
goods.Remove(good);
}
}
<DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick" />
(2)取消自动生成列,手动绑定到相应字段
<DataGrid AutoGenerateColumns="False" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="Info" Binding="{Binding Info}" Width ="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
最后一列设置Width ="*"是为了取消空白列。
(3)后台代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = AccessDAL.OleDbHelper.ExecuteDataTable("SELECT * from 表名").DefaultView;
}
//双击DataGrid,显示相应信息
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = datagrid.SelectedItem as DataRowView;
MessageBox.Show(row["id"].ToString());
}
//如果绑定到对象集合,如ObservableCollection<Employee>,代码如下:
ObservableCollection<Employee> col;
public EmployeeManage()
{
InitializeComponent();
col = new ObservableCollection<Employee>();
col.Add(new Employee() { Id = 1, Name = "张三", Info = ********* });
col.Add(new Employee() { Id = 2, Name = "李四", Info = ********* });
datagrid.ItemsSource = col;
}
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Employee emp=datagrid.SelectedItem as Employee;
MessageBox.Show(emp.Id.ToString());
}
(4)删除选中的多行数据
private void Delete_Click(object sender, RoutedEventArgs e)
{
for (int i = datagrid.SelectedItems.Count - 1; i >= 0; i--)
{
Good good = datagrid.SelectedItems[i] as Good;
goods.Remove(good);
}
}
相关文章推荐
- WPF DataGrid使用简介
- 【项目实战】---Easyui datagrid detailview使用简介
- WPF使用DataGridComboBoxColumn完成绑定
- WPF DataGrid 中的DataGridComboBoxColumn 使用
- WPF--DataGrid的使用
- [WPF]DataGridTemplateColumn使用ComboBox绑定Dictionary细节记录
- WPF中DataGrid使用初步
- WPF DataGrid DataGrid.Columns 使用笔记
- WPF使用DataGridComboBoxColumn完成绑定
- wpf 中DataGrid 控件的样式设置及使用
- WPF中DataGrid使用初步
- WPF中Datagrid其中一列使用图片显示
- WPF中DataGrid使用初步
- WPF学习之使用DataGrid
- WPF的DataGrid的使用技巧—列宽调整和列排序问题
- wpf:DataGrid使用
- WPF中DataGrid的空白行使用方法
- WPF中DataGrid使用自定义列绑定数据
- 『WPF』DataGrid的使用
- WPF使用MVVM完成DataGrid数据绑定带全选