0

オブジェクトのリストがあります。私のクラスは次のようになります。

    class Line
    {
    public string A { get; set; }
    public object B { get; set; }
    public string C { get; set; }
    }

class More_B
{
    public Information Information { get { return _Information; }}
    public Description Description { get { return _Description ; }}

    private Information  _Information  = new Information();
    private Description  _Description  = new Description ();
}

class Information
{
    public string Name { get; set;}
    public string Type { get; set;}
    public string Further_Info { get; set;}
}

最後のメインクラスでは、Lineこのようにクラス内のすべてのデータを取得します。

More_B info = new More_B();

 Line main = new Line();
 main.A = "Information about device";
 main.B = info;
 main.C = "Some more information could be display here";
 list3.Add(main);

最後に、3つのフィールドすべてを含むリストを取得しますA B C。文字列値がACにありB、デバッグ モードでチェックすると、情報オブジェクトがあり、その中に と のようなすべてのフィールドがName TypeありFurther_Infoます。

後で、これらの値「Name」「Type」および「Further_Info」に基づいてこの列をフィルタリングしたいのですが、この方法で試しましたが、機能しません。(RowFilter を使用できるように、リストを DataView にバインドしました)。

view2.RowFilter = "Line like '%" + textBox2.Text + "%'";

どうすれば正しくできますか?

私はC#が初めてなので、何か間違ったことをした場合はご容赦ください。理解が難しい場合はお知らせください。自分が行ったことを説明するコードをさらに投稿しようと思います。

編集:このステートメントを使用するview2.RowFilter = "Line like '%" + textBox2.Text + "%'";と、エラーが発生しますCannot perform 'Like' operation on read_display.More_B and System.String.

EDIT2:クラスのデータテーブルを作成するためのメソッドがさらに2つあり、次のようにします:DataTable ListAsDataTable2 = BuildDataTable2<Line>(list3); DataView ListAsDataView2 = ListAsDataTable2.DefaultView; this.dataGridView4.DataSource = view2 = ListAsDataView2;

4

2 に答える 2

3

この方法で試してください:

public class Line
{
   public string A {get;set;}
   public More_B B {get;set;}
   public string C {get;set;}         

}

public class Information
{
   public string Name { get; set;}
   public string Type { get; set;}
   public string Further_Info { get; set;}
}

public class More_B:Information
{
   public string CusotmField {get;set;}
}

var B_Object = new More_B (Name = "The B", Type = "Some type", Further_Info = "More Info");
IList<More_B> B_List=new List<More_B>();
var searchObj = B_List.Where(x => x.Name=="The B").FirstOrDefault();
于 2013-08-08T10:07:31.757 に答える