当前位置: 首页 > news >正文

如何从 WPF 控件 DataGrid 中删除多余的列 - 指南

这里我们都知道 WPF Datagrid 总在末尾带有额外的列,故而很多人问为什么 Datagrid 中会展示额外的列,例如:DB(数据库)表或模型类具备 5 列,但在源添加列后显示 6 列,没有标题。

这里,Datagrid 是 WPF 框架中的一个 XAML 控件,DataGrid 本身无法显示列数,因此每次传递 itemsource 值后,它都会默认显现带有值的列。这个额外的列取决于它所引用的标题和列的宽度,而这恰恰是 DataGrid 的宽度分布。

若是您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

下面我将向您展示如何应对这个问题。

创建 WPF 应用程序
将 DataGridWindow.xaml 项目添加到项目中。
将 DataGrid 控件添加到新计划中

添加用于在 DataGrid 控件中展示数据的后端代码

namespace MVVMLightSample_DotNetCore8.View
{
/// <summary>
/// Interaction logic for DataGridWindow.xaml
/// </summary>
public partial class DataGridWindow : Window
{
public DataGridWindow()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
List<Product> product = new List<Product>();
product.Add(new Product { ProductID = 1, ProductName = "Laptop", ProductDescription = "Dell Laptop", ProductPrice = 50000, ProductStock = 10 });
product.Add(new Product { ProductID = 2, ProductName = "Mobile", ProductDescription = "Samsung Mobile", ProductPrice = 20000, ProductStock = 20 });
product.Add(new Product { ProductID = 3, ProductName = "Tablet", ProductDescription = "Lenovo Tablet", ProductPrice = 15000, ProductStock = 30 });
product.Add(new Product { ProductID = 4, ProductName = "Desktop", ProductDescription = "HP Desktop", ProductPrice = 40000, ProductStock = 40 });
product.Add(new Product { ProductID = 5, ProductName = "Printer", ProductDescription = "Canon Printer", ProductPrice = 10000, ProductStock = 50 });
product.Add(new Product { ProductID = 6, ProductName = "Scanner", ProductDescription = "Epson Scanner", ProductPrice = 8000, ProductStock = 60 });
product.Add(new Product { ProductID = 7, ProductName = "Keyboard", ProductDescription = "Logitech Keyboard", ProductPrice = 500, ProductStock = 70 });
product.Add(new Product { ProductID = 8, ProductName = "Mouse", ProductDescription = "Dell Mouse", ProductPrice = 300, ProductStock = 80 });
Datagrid1.ItemsSource = product;
}
}
}

现在构建项目,数据将填充 DataGrid 控件,但是,额外的列仍然显示在 DataGrid 内部,黄色突出显示区域。

现在的问题是如何删除该多余的列。

在DataGridWindow.xaml的Xaml代码中,需要在DataGrid控件中添加一个小属性“ColumnWidth="*”。

现在重新构建代码并执行。下面的窗口将显示多余的列已从 DataGrid 控件中删除,并且数据已完全填充到 DataGrid 中。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

http://www.rkmt.cn/news/49052.html

相关文章:

  • 2025年靠谱的耐磨安全绳实力厂家TOP推荐榜
  • nats-account-server nats 的accout服务
  • 2025亚沟粘豆包供应商怎么选购
  • 从零开始学java--二叉树和哈希表
  • 2025年比较好的速降安全带高评价厂家推荐榜
  • 2025年EGUOO营养包:深度解析科研配方与人群适配逻辑
  • 开关箱端子统计开发随笔
  • 2025年热门的隐藏珠宝柜滑轨最新TOP厂家排名
  • 2025年口碑好的阻尼珠宝柜滑轨厂家最新推荐权威榜
  • Spring AI 1.0 GA 深度解析:Java生态的AI革命已来
  • 2025年北京搬家公司联系电话推荐:顺达搬家灵活预约速达
  • 2025年北京搬家公司联系电话推荐:华宇兄弟专业品质保障
  • 2025年EGUOO胃肠消化酶深度解析:科研专利与多维功效的交叉验证
  • 2025年热门的重型反弹器优质厂家推荐榜单
  • 2025年质量好的平板铰链厂家最新热销排行
  • 2025年质量好的下翻门上翻门厂家推荐及采购参考
  • 2025年靠谱的三维平板铰链厂家推荐及采购指南
  • 2025年评价高的餐饮广告灯箱厂家最新推荐排行榜
  • 【GitHub每日速递 20251114】3 步实现实时深伪换脸!Deep - Live - Cam 让你秒变任何人
  • 2025年口碑好的茶叶烘干网带用户好评厂家排行
  • 高效办公:用SQL*Loader轻松实现Excel数据入库
  • ヒッチコック
  • 2025年EGUOO睡眠片用法深度解析:权威拆解服用细节与科学逻辑
  • 2025年EGUOO睡眠片用法深度解析:权威拆解剂量、时机与个体适配策略
  • 软件研发 --- 开发时经常遇到的问题
  • 考试小记
  • 在AI技术唾手可得的时代,挖掘新需求成为制胜关键——某知名网页特效库需求探索
  • 价值原语化:一个独立的方法论
  • 联合索引 最左命中
  • 2025广东封闭式管理学校最新TOP5评测:重塑少年言行,见证成长蜕变