C# Propertygrid 默认值类型转换器
我有一个带有下拉框的属性网格。在我的应用程序中,用户可以单击一个块,然后该块的属性显示在属性网格中。但他们第一次单击某个块时,下拉列表中会显示无效值 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过 DefaultValueAttribute在系统组件模型中?
来自 MSDN:
Have you tried the DefaultValueAttribute in System.ComponentModel?
From MSDN:
在将对象显示在 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.