【问题标题】:Change property type in DataGrid style in the Paint function?在 Paint 函数中更改 DataGrid 样式的属性类型?
【发布时间】:2012-07-26 06:27:44
【问题描述】:

我有一个映射到 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 上收到关于无效参数异常的错误,我认为这是因为类型,如果我设置另一个整数,它运行正常。

【问题讨论】:

    标签: c# sql datagrid compact-framework


    【解决方案1】:

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

    如果您想更改字段以显示一些文本,您可能需要更改 SqlDataAdapter 中使用的 SQL,以便 rowNum 显示为文本。

    例如,代替这个 SQL:

    SELECT ID1
    FROM Table1
    

    您可以使用这个版本的 SQL:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 2013-11-04
      • 2017-12-14
      • 2019-03-14
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多