0

我有一个映射到 SQLDataAdapter 的数据网格,其中一行是一个整数,它是一个对应于字符串的 id。

我想在绘画功能中做的是:

protected override void Paint( Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight )
  {
     int id = ( ( int )this.PropertyDescriptor.GetValue( source.List[ rowNum ] ) );

     int oldValue = id;
    this.PropertyDescriptor.SetValue( source.List[ rowNum ], "Some Text" );// m_textDataMapping[ id ] );
     base.Paint( g, bounds, source, rowNum, backBrush, foreBrush, alignToRight );
     this.PropertyDescriptor.SetValue( source.List[ rowNum ], oldValue );
}

我在 this.PropertyDescriptor.SetValue 上收到关于无效参数异常的错误,我认为这是因为类型,如果我设置另一个整数,它运行正常。

4

1 回答 1

1

我不使用PropertyDescriptor,但它的PropertyType是只读的。因此,如果它是整数,则不能将文本写入该值。

如果要更改字段以显示某些文本,则可能需要更改SqlDataAdapter中使用的 SQL,以便将rowNum其显示为文本。

例如,而不是这个 SQL:

SELECT ID1
FROM Table1

您可以使用此版本的 SQL:

SELECT CAST(ID1 AS nVarChar(50)) AS 'ID1'
FROM Table1

现在,您可以将rowNum其视为文本字段。

然而,这确实是有代价的。现在,您还必须将读取的值从字符串值转换为整数值。

于 2012-07-26T16:50:32.687 回答