隐藏 DataGrid 中 DataSource 为 DataTable 的 DataColumn (Visual C#)

代码:隐藏 DataGrid 中 DataSource 为 DataTable 的 DataColumn (Visual C#)

本示例隐藏在现有 Windows 窗体 DataGrid 控件中显示的 DataTable 对象的“X”列。

示例
private void HideColumnOfDataSet(){    System.Data.DataTable points = new System.Data.DataTable("Points");    points.Columns.Add(new DataColumn("X", typeof(int)));    points.Columns.Add(new DataColumn("Y", typeof(int)));    points.Rows.Add(new object[]{1, 2});    points.Rows.Add(new object[]{3, 5});    dataGrid1.DataSource = points;    DataGridTableStyle tableStyle = new DataGridTableStyle();    tableStyle.MappingName = "Points";    dataGrid1.TableStyles.Add(tableStyle);    dataGrid1.TableStyles["Points"].GridColumnStyles["X"].Width = 0;}
编译代码

本示例需要:

  • 具有名为 dataGrid1 的 DataGrid 控件的 Windows 窗体。

如果数据源是 DataSet 对象,则将 DataGrid  DataMember 属性设置为该表的名称。

类型化数据集中的 DataTable  DataColumn 对象还具有字符串类型的名称。若要查找表的名称,请查看表的 Name 属性。若要查找DataColumn 的名称,请查看列的 Name 属性。

可靠编程

以下情况可能会导致异常:

  • MappingName 属性与 DataTableNullReferenceException 类)的名称不匹配。
  • 在将 TableStyle 添加到 DataGrid.TableStyles 集合前,先从 GridColumnStyles 集合中检索某一项。在将 TableStyle 对象添加到TableStyles 集合(NullReferenceException 类)中时,就会填充 GridColumnStyles 集合。
  • 添加到 DataGrid.TableStyles 集合中的 TableStyle 没有唯一的 MappingNameArgumentException 类)。
  • 在访问 GridColumnStyles 集合(NullReferenceException 类)之前,不会设置 DataGrid  DataSource 属性。
请参见

Windows 窗体示例主题 | 隐藏 DataGrid 中 DataSource 为数组的 DataColumn |

转载于:https://www.cnblogs.com/axinno1/articles/1892188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值