C# Propertygrid 默认值类型转换器

发布于 2024-08-17 09:11:35 字数 1795 浏览 4 评论 0原文

我有一个带有下拉框的属性网格。在我的应用程序中,用户可以单击一个块,然后该块的属性显示在属性网格中。但他们第一次单击某个块时,下拉列表中会显示无效值 (0)。如何确保显示有效值?

下面是 TypeConverter 的一些代码:

public class DynamicFormScreenId : Int32Converter
{

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        Database database = new Database();
        database.StoredProcedureName = "SP naam";

        int[] id = null;
        if (database.ExecuteStoredProcedure())
        {
            int totalIds = database.DataTable.Rows.Count; 
            id = new int[totalIds];

            for (int i = 0; i < totalIds; i++)
            {
                id[i] = Convert.ToInt32(database.DataTable.Rows[i][0]);
            }
        }

        return new StandardValuesCollection(id);
    }

    public override bool GetStandardValuesExclusive(
                       ITypeDescriptorContext context)
    {
        return true;
    }
}

以及我的类中的属性:

[TypeConverter(typeof(DynamicFormScreenId)),
CategoryAttribute("Screen Settings"),
Description("The id of the screen")]
public int ScreenId
{
    get { return _screenId; }
    set { _screenId = value; }
}

解决方案

我在构造函数中设置了 ScreenId 的默认值:

private void Constructor(string name)
{
    _controlName = name;

    // Set screenId default value
    Database database = new Database("connectionstring");
    database.StoredProcedureName = "mySP";

    if (database.ExecuteStoredProcedure())
    {
        if (database.DataTable.Rows.Count > 0)
        {
            _screenId = Convert.ToInt32(database.DataTable.Rows[0]["id"]);
        }
    }
}

I have a propertygrid with a dropdown box. In my application a user can click on a block and then the properties of that block are shown in a propertygrid. But the first time they click on a block an invalid value (0) is shown in the dropdown. How can I make sure that a valid value is shown?

Here is some code of the TypeConverter:

public class DynamicFormScreenId : Int32Converter
{

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        Database database = new Database();
        database.StoredProcedureName = "SP naam";

        int[] id = null;
        if (database.ExecuteStoredProcedure())
        {
            int totalIds = database.DataTable.Rows.Count; 
            id = new int[totalIds];

            for (int i = 0; i < totalIds; i++)
            {
                id[i] = Convert.ToInt32(database.DataTable.Rows[i][0]);
            }
        }

        return new StandardValuesCollection(id);
    }

    public override bool GetStandardValuesExclusive(
                       ITypeDescriptorContext context)
    {
        return true;
    }
}

And the property in my class:

[TypeConverter(typeof(DynamicFormScreenId)),
CategoryAttribute("Screen Settings"),
Description("The id of the screen")]
public int ScreenId
{
    get { return _screenId; }
    set { _screenId = value; }
}

SOLUTION

I set the default value of ScreenId in the constructor:

private void Constructor(string name)
{
    _controlName = name;

    // Set screenId default value
    Database database = new Database("connectionstring");
    database.StoredProcedureName = "mySP";

    if (database.ExecuteStoredProcedure())
    {
        if (database.DataTable.Rows.Count > 0)
        {
            _screenId = Convert.ToInt32(database.DataTable.Rows[0]["id"]);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-08-24 09:11:35

您是否尝试过 DefaultValueAttribute在系统组件模型中?

来自 MSDN:

您可以创建一个 DefaultValueAttribute
具有任何价值。会员默认
value 通常是其初始值。
视觉设计师可以使用默认的
value 来重置成员的值。

private bool myVal=false;

[DefaultValue(false)]
 public bool MyProperty {
    get {
       return myVal;
    }
    set {
       myVal=value;
    }
 }

Have you tried the DefaultValueAttribute in System.ComponentModel?

From MSDN:

You can create a DefaultValueAttribute
with any value. A member's default
value is typically its initial value.
A visual designer can use the default
value to reset the member's value.

private bool myVal=false;

[DefaultValue(false)]
 public bool MyProperty {
    get {
       return myVal;
    }
    set {
       myVal=value;
    }
 }
指尖上得阳光 2024-08-24 09:11:35

在将对象显示在 PropertyGrid 中之前,您必须先分配该对象的 ScreenId 属性值。实际上,您必须运行数据库查询两次。一旦知道要为 ScreenId 分配什么值,再次在类型转换器中。

You will have to assign the object's ScreenId property value before you show it in the PropertyGrid. In effect, you have to run the dbase query twice. Once to know what value to assign to ScreenId, again in the type converter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文