Rus*_*kwa 5 .net c# datagridview windows-forms-designer winforms
我想在设计模式/设计时显示 DataGridView 行。有没有办法实现这一目标?
我曾尝试使用此函数:datagridview1.Rows.Add();在控件的构造函数中,但行及其项目仅在runtime中显示。
为什么我需要在显示行及其项目的原因,设计时是我定制的外观DataGridView控件。因此,每次我进行更改,例如更改AlternatingRowColor、RowFont 等时,我都必须运行控件才能查看更改。
这会消耗时间。
如果我可以在设计时/设计模式中显示所有行,那么我将能够在我制作它们时立即看到更改,这将节省大量时间。
我定制的外观DataGridView控件在Windows窗体和我使用C# 。
我不是在代码中定义控件,而是将控件从“控件”选项卡拖放到 Winform 上。
谢谢。
我将一个设计动词添加到上下文菜单中DataGridView,并按所需的行为分配它。例如(作为使用虚拟数据进行预览的起点):
using System;
using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyDataGridView : DataGridView
{
private IDesignerHost designerHost;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (DesignMode && Site != null)
{
designerHost = Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
var designer = (ControlDesigner)designerHost?.GetDesigner(this);
designer?.Verbs?.Add(new DesignerVerb("Preview with dummy data", (o, a) =>
{
//Some logic to add dummy rows, just for example
this.Rows.Clear();
if (Columns.Count > 0)
{
var values = Columns.Cast<DataGridViewColumn>()
.Select(x => GetDummyData(x)).ToArray();
for (int i = 0; i < 2; i++)
Rows.Add(values);
}
}));
designer?.Verbs?.Add(new DesignerVerb("Clear data", (o, a) =>
{
this.Rows.Clear();
}));
}
}
private object GetDummyData(DataGridViewColumn column)
{
//You can put some logic to generate dummy data based on column type, etc.
return "Sample";
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您将看到两个菜单项添加到上下文菜单中,并且通过单击“使用虚拟数据预览”,您将看到在设计时添加到控件中的两个虚拟行:
| 归档时间: |
|
| 查看次数: |
2091 次 |
| 最近记录: |