简体   繁体   English

将具有自定义对象类型的列的DataGrid绑定到DataTable

[英]Bind DataGrid to DataTable haveing columns of type Custom Objects

I have DataTable and I am trying to bind DataGrid with DataTable. 我有DataTable,并且正在尝试将DataGrid与DataTable绑定。 But my columns are not of simple type, they are of custom Objects. 但是我的列不是简单类型,它们是自定义对象。

public class Node
    {
        public string Name { get; set; }
        public int Id { get; set; }

        public Node(string name, int id)
        {
            Name = name;
            Id = id;
        }
    }

public class NodeBool
    {
        public bool Name { get; set; }
        public int Id { get; set; }

        public Node(bool name, int id)
        {
            Name = name;
            Id = id;
        }
    }


     <DataGrid Name="data1" AutoGenerateColumns="True" ItemsSource="{Binding MyDataTable}" />

The columns are not known before execution. 在执行之前,这些列是未知的。 They are known at runtime based on a list, ad list is used to add columns in the datatable 它们在运行时基于列表是已知的,广告列表用于在数据表中添加列

When I bind it to datagrid, For each row under column full class name is displayed. 当我将其绑定到datagrid时,将显示完整类名称下的每一行。 I want to bind it to class property and depending on the property checkbox (bool) or textbox (string) should be displayed. 我想将其绑定到类属性,并取决于属性复选框(布尔)或文本框(字符串)应显示。

I am following MVVM model. 我正在关注MVVM模型。

you have to use in below mentioned code. 您必须在下面提到的代码中使用。

public class NodeBool
{
    public bool Name { get; set; }
    public int Id { get; set; }

    public NodeBool(bool name, int id)
    {
        Name = name;
        Id = id;
    }
}

then you have to declare the ObservableCollection like 那么你必须像这样声明ObservableCollection

 private ObservableCollection<NodeBool> _MyData=new ObservableCollection<NodeBool>();

    public ObservableCollection<NodeBool> MyData
    {
        get { return _MyData; }
        set { _MyData = value; RaisePropertyChanged("MyData"); }
    }

and then you have to enter the value in collecton like 然后你必须在collecton中输入值

  MyData.Add(new NodeBool(true,1));

and your datagrid look like 你的数据网格看起来像

<DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn MinWidth="150" Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn MinWidth="180" Header="Id" Binding="{Binding Id}"/>
        </DataGrid.Columns>
    </DataGrid>

What you can do is Change your Code Likes this 您可以做的就是像这样更改代码

 <DataGrid Name="data1" AutoGenerateColumns="True" ItemsSource="{Binding Path=.}" />

and in Code Behind you can do is 在“代码隐藏”中,您可以做的是

Create a one DataTable and Add Columns in that and add Data in that 创建一个DataTable并在其中添加列,然后在其中添加数据

 DataTable dataTable=new DataTable();

//for loop getting all properties of object using reflection dataTable.Columns.Add(item.Name); // for循环使用反射dataTable.Columns.Add(item.Name);获取对象的所有属性

//for loop for gettting all the information in dataTable //用于获取数据表中所有信息的循环

DataRow  dr;

//add each value like this //像这样添加每个值

dr=dataTable.NewRow(); DR = dataTable.NewRow();

dr[propertyName] = object.Propertyname; dr [propertyName] = object.Propertyname;

dataTable.Rows.Add(dr);
   dataTable.AcceptChanges();

// then bind that table to datagrid. //然后将该表绑定到datagrid。

   data1.ItemSource = dataTable.DefaultView 

or 要么

   data1.DataContext = dataTable; 

Exact Code Base will look like this Here I assume that your source of information will be list of class of type(Node class) it will convert it to respective dataTable and will show the datagrid 确切的代码库将如下所示:在这里,我假设您的信息源将是类型(Node类)的类的列表,它将其转换为相应的dataTable并显示datagrid

public void ConvertListToDataTable()
{
    DataTable dt = new DataTable();
    Node obj = new Node();
    //here obj is instance of your node type
    foreach (var item in obj.GetType().GetProperties())
    {
        dt.Columns.Add(item.Name);
    }

    List<Node> objDetails = new List<Node>();
    Node objNode = new Node();
    objNode.Name = true;
    objNode.Id = "Test B";

    Node objNode1 = new Node();
    objNode1.Name = false;
    objNode1.Id = "Test B 1";

    objDetails.Add(objNode);
    objDetails.Add(objNode1);

    DataRow dr;
    for (int i = 0; i < objDetails.Count; i++)
    {
        dr = dt.NewRow();
        foreach (var item in obj.GetType().GetProperties())
        {
            dr[item.Name] = objDetails[i].GetType().GetProperty(item.Name).GetValue(objDetails[i], null);
        }
        dt.Rows.Add(dr);
    }

    dt.AcceptChanges();
    data1.ItemsSource = dt.DefaultView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM