如何使用附加属性和方法在 C# 中扩展 DataRow 和 DataTable? [英] How to extend DataRow and DataTable in C# with additional properties and methods?

查看:18
本文介绍了如何使用附加属性和方法在 C# 中扩展 DataRow 和 DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义 DataRow,其中包含一个名为 IsCheapest 的属性.

I would like create a custom DataRow that will have -let's say- a propery called IsCheapest.

public class RateDataRow : DataRow
{
    protected internal RateDataRow(DataRowBuilder builder) : base(builder)
    {
    }

    public bool IsCheapest { get; set ;}
}

我想要一个只包含 ***RateDataRow***s 的新数据表,以便 .NewDataRow() 将 RateDataRow 实例作为新行返回.

And I want to have a new DataTable that contains only ***RateDataRow***s so that .NewDataRow() returns RateDataRow instance as a new row.

扩展 DataTable 的类的实现应该是什么?

what should be the implementation on the class that extends DataTable?

谢谢,

推荐答案

根据您的问题,不清楚您是否熟悉类型化数据集.它们基本上就是您所要求的.

From your question it's not clear if you are familiar with Typed Datasets. They basically are what you are asking for.

您可以使用内置向导创建基于 XSD 的类型化数据集(并且 XSD 从 Db 架构中提取).在 WinForms 项目中,选择添加数据源"并按照步骤操作.

You could use the built-in wizards to create a Typed Dataset based on an XSD (and the XSD is extracted from the Db schema). In a WinForms project, select "Add DataSource" and follow the steps.

即使您不想使用该模型,您也可以从属性、部分类等的代码中借用.

Even if you don't want to use that model you could borrow from the code for properties, partial classes etc.

明智的做法是要么使用该模型,要么非常接近它.

It would be wise to either use that model or stay very close to it.

这篇关于如何使用附加属性和方法在 C# 中扩展 DataRow 和 DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆