c# - 从 VS 2012 设置 AutoGenerateColumns 属性

标签 c# .net winforms visual-studio-2012

刚才我遇到了这个奇怪的问题,我想知道是否有我在 VS2012 中遗漏的东西来解决它。我正在使用 MDI Windows 窗体,我有这个 dataGridView,我从数据库中检索数据,然后使用数据库中的数据设置网格的 dataSource 属性。但是即使我使用一个旧项目作为这个项目的脚手架,现在我得到了 VS2012 中设计的 dataGridView 并且在它旁边我从数据库中获取数据,就好像它是我的一部分 dataGridView 设计,除了标题列是来自数据库的列名。

但是我做了一些研究,发现实际上这个问题很容易通过将 AutoGenerateColumns 属性设置为 false 来解决。我在 VS2012 设计器中找不到这个属性。现在我正在使用这个:

        dgvClients.AutoGenerateColumns = false;
        dgvClients.DataSource = mainGridInfoList;

load 事件中,但我宁愿使用设计器(当然如果可能的话)并删除这一行 - dgvClients.AutoGenerateColumns = false; 从我的代码。问题是我看不到 dataGridView 的确切名称的属性,所以我想知道 - 名称是否已更改?这有点奇怪,因为我仍然可以在我的代码中使用它,或者 IDE 设计器从来没有办法做到这一点,它必须明确地写在源代码中?

附言

对于这个特定问题,使用 IDE 设计器并不是很好的决定(至少我这么认为),但这里有一个打印屏幕,可以向您展示如何从 VS2012 设计器设置此属性。

AutoGenerateColumn

最佳答案

我在文档中找到了一些可能对您的问题有所帮助的内容。

来自 DataGridView.AutoGenerateColumns Property :

Note:

Setting the DataSource in the Windows Forms Designer automatically sets the AutoGenerateColumns property to false and generates code to add and bind a column for each property in the data source. The code that is generated at design-time is equivalent to the manually added code shown in the following example. It is not the same as the auto-generation of columns at run-time that occurs when the AutoGenerateColumns property is set to true.

关于c# - 从 VS 2012 设置 AutoGenerateColumns 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18157192/

相关文章:

.net - DoDragDrop和MouseUp

c# - 将数据表转换为数据读取器

winforms - .NET (WinForms) 具有折叠/展开功能的网格控件

c# - 动态加载程序集时加载绑定(bind)从 *.dll.config 重定向

c# - 创建自定义列类型?

c# - 在 ASP.NET Core 和 .NET Core 控制台项目中使用相同的 `.resx` 文件

c# - 流的通用 IDisposable 包装器 - 捕获所有异常的替代方法

c# - 如何检测 WinForms 面板是否滚动到末尾?

c# - Radpanelbar 折叠/展开问题

c# - 如何编写本地化的屏幕键盘