如何使用附加属性和方法扩展 C# 中的 DataRow 和 DataTable?

发布于 2024-09-06 21:09:22 字数 375 浏览 3 评论 0原文

我想创建一个自定义 DataRow,它有一个名为 IsCheapest 的属性。

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

    public bool IsCheapest { get; set ;}
}

我想要一个仅包含 ***RateDataRow*** 的新 DataTable,以便 .NewDataRow() 将 RateDataRow 实例作为新行返回。

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

谢谢,

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 ;}
}

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

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

Thanks,

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

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

发布评论

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

评论(3

行雁书 2024-09-13 21:09:22

我知道这是一篇旧文章,但我无法使上面的示例发挥作用。我遇到了类似的问题,所以很想找到解决方案。经过一番研究后,我发现以下方法有效:

class Program
{
    static void Main(string[] args)
    {
        MyDataTable t1 = new MyDataTable();

        t1.Columns.Add(new DataColumn("Name", typeof(string)));
        t1.Columns.Add(new DataColumn("DateOfBirth", typeof(DateTime)));

        MyDataRow r1 = t1.NewRow() as MyDataRow;
        r1["Name"] = "Bob";
        r1["DateOfBirth"] = new DateTime(1970, 5, 12);
        t1.Rows.Add(r1);
    }
}

[Serializable]
public class MyDataTable : DataTable
{
    public MyDataTable()
        : base()
    {
    }

    public MyDataTable(string tableName)
        : base(tableName)
    {
    }

    public MyDataTable(string tableName, string tableNamespace)
        : base(tableName, tableNamespace)
    {
    }

    /// <summary>
    /// Needs using System.Runtime.Serialization;
    /// </summary>
    public MyDataTable(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    protected override Type GetRowType()
    {
        return typeof(MyDataRow);
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new MyDataRow(builder);
    }
}

[Serializable]
public class MyDataRow : DataRow
{
    public bool MyPropertyThatIdicatesSomething { get; private set; }

    public MyDataRow()
        : base(null)
    {
    }

    public MyDataRow(DataRowBuilder builder)
        : base(builder)
    {
    }
}

I know this is an old post now but I couldn't get the example above to work. I had a similar problem so was keen to find a solution. After a bit of research I found the following to work:

class Program
{
    static void Main(string[] args)
    {
        MyDataTable t1 = new MyDataTable();

        t1.Columns.Add(new DataColumn("Name", typeof(string)));
        t1.Columns.Add(new DataColumn("DateOfBirth", typeof(DateTime)));

        MyDataRow r1 = t1.NewRow() as MyDataRow;
        r1["Name"] = "Bob";
        r1["DateOfBirth"] = new DateTime(1970, 5, 12);
        t1.Rows.Add(r1);
    }
}

[Serializable]
public class MyDataTable : DataTable
{
    public MyDataTable()
        : base()
    {
    }

    public MyDataTable(string tableName)
        : base(tableName)
    {
    }

    public MyDataTable(string tableName, string tableNamespace)
        : base(tableName, tableNamespace)
    {
    }

    /// <summary>
    /// Needs using System.Runtime.Serialization;
    /// </summary>
    public MyDataTable(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    protected override Type GetRowType()
    {
        return typeof(MyDataRow);
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new MyDataRow(builder);
    }
}

[Serializable]
public class MyDataRow : DataRow
{
    public bool MyPropertyThatIdicatesSomething { get; private set; }

    public MyDataRow()
        : base(null)
    {
    }

    public MyDataRow(DataRowBuilder builder)
        : base(builder)
    {
    }
}
孤云独去闲 2024-09-13 21:09:22

DataTable 公开 GetRowType 虚拟方法,在派生类中重写该方法。任何尝试添加错误类型的行都会引发异常:

class Program
{
    static void Main(string[] args)
    {
        MyDataTable t = new MyDataTable();
        t.Rows.Add(t.NewRow()); // <-- Exception here, wrong type (base doesn't count).
    }
}

public class MyDataTable : DataTable
{
    public MyDataTable()
        : base()
    { }

    protected override Type GetRowType()
    {
        return typeof(MyDataRow);
    }
}

public class MyDataRow : DataRow
{
    public MyDataRow()
        : base(null)
    { }
}

The DataTable exposes a GetRowType virtual method, override this in a derived class. Any attempts to add a row of the wrong type will throw an exception:

class Program
{
    static void Main(string[] args)
    {
        MyDataTable t = new MyDataTable();
        t.Rows.Add(t.NewRow()); // <-- Exception here, wrong type (base doesn't count).
    }
}

public class MyDataTable : DataTable
{
    public MyDataTable()
        : base()
    { }

    protected override Type GetRowType()
    {
        return typeof(MyDataRow);
    }
}

public class MyDataRow : DataRow
{
    public MyDataRow()
        : base(null)
    { }
}
情绪操控生活 2024-09-13 21:09:22

从您的问题来看,尚不清楚您是否熟悉类型化数据集。它们基本上就是您所要求的。

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

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

明智的做法是使用该模型或保持非常接近它。

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

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.

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