DataGrid.SetDataBinding 示例

DataGrid.SetDataBinding 方法 在运行时设置 DataSource 和 DataMember 属性。

参数 dataSource System.Windows.Forms.DataGrid 控件的数据源。 dataMember DataMember 字符串,它指定该表要绑定到 DataSource 属性所返回的对象中。  异常 异常类型条件 ArgumentException 一个或多个参数无效。 ArgumentNullException dataSource 参数为 空引用(在 Visual Basic 中为 Nothing)。  备注 在运行时必须使用 SetDataBinding 方法来重置 DataSource 属性。有关设置有效数据源的更详细信息,请参见 DataSource 属性。通过将 DataView 用作数据源并将 AllowNew 属性设置为 false,可以创建网格,该网格允许用户编辑数据但禁止他们添加新行。当 DataSource 为 DataView 或 DataTable 时,请将 DataMember 设置成空字符串 ("")。  

 示例    示例 下面的代码示例将 DataSource 和 DataMember 分别设置为 DataSet 中的 DataSet 和 DataTable。  

 private void BindControls()

{    // Creates a DataSet named SuppliersProducts.    DataSet SuppliersProducts = new DataSet("SuppliersProducts");    // Adds two DataTable objects, Suppliers and Products.    SuppliersProducts.Tables.Add(new DataTable("Suppliers"));    SuppliersProducts.Tables.Add(new DataTable("Products"));    // Insert code to add DataColumn objects.    // Insert code to fill tables with columns and data.    // Binds the DataGrid to the DataSet, displaying the Suppliers table.    dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers"); } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid是一种用于显示数据的控件,而DataGrid.Columns则是DataGrid中的列集合。你可以通过设置DataGrid.Columns的Background属性来改变整个列的背景颜色。 在WPF中,你可以使用多种方式来设置DataGrid.Columns的Background属性,包括使用静态资源、绑定属性、使用样式等。以下是一些示例代码: 1. 使用静态资源设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="{StaticResource MyBrush}" /> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> ``` 2. 使用绑定属性设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="{Binding DataContext.Column1HeaderBackground, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> ``` 3. 使用样式设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Resources> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="LightGray" /> </Style> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" /> </DataGrid.Columns> </DataGrid> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值