Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用列表和子列表填充C#ListView_C#_List_Listview_Struct - Fatal编程技术网

用列表和子列表填充C#ListView

用列表和子列表填充C#ListView,c#,list,listview,struct,C#,List,Listview,Struct,嗨,我有2个结构,我想填充一个列表视图,但它的列我想使用列表的产品名称: 我的listview我想看到如下内容: firstname ---- lastname ---- phone ---- CocaCola ---- Sprite -- TotalPrice John Jamson 1234556601 1 2 100$ 但如果我有另一个代理人,比如: myAgent.fistname="John2";

嗨,我有2个结构,我想填充一个列表视图,但它的列我想使用
列表的产品名称

我的listview我想看到如下内容:

 firstname ---- lastname ---- phone ---- CocaCola ---- Sprite -- TotalPrice 
 John           Jamson     1234556601        1           2          100$
但如果我有另一个代理人,比如:

myAgent.fistname="John2";
myAgent.lastname="Jamson2";
myAgent.phone = "1234556602";
myAgent.TotalPrice="120$";
myAgent.transaction_id="2";
myAgent.Products={["1","CocaCola"],["2","Sprite"],["1","Lemon"]};
我想在我的listview中看到:

 firstname- lastname - phone - CocaCola -- Sprite ----- lemon -TotalPrice 
 John       Jamson  1234556601     1          2           -       100$
 John2      Jamson2 1234556602     1          2           1       120$

我建议创建一个适配器类,进行必要的转换,使数据适合列表视图:

public class AgentSalesDetailListViewAdapter
{
    private List<AgentSalesDetailsList> _details;
    private ListView                    _view;
    private ISet<string>                _productNames = new SortedSet<string>();
    private DataTable                   _data         = new DataTable();

    public AgentSalesDetailListViewAdapter(List<AgentSalesDetailsList> details, ListView view)
    {
        _details = details;
        _view    = view;
    }

    public void SetUpView()
    {
        PopulateProductNames();
        PrepareDataColumns();
        FillData();
        FillView();
    }

    private void PopulateProductNames()
    {
        _productNames.Clear();

        // collect all product names into a single set
        foreach ( AgentSalesDetailsList detail in _details )
        {
            foreach ( SalesProducts product in detail.Products )
            {
                _productNames.Add(product.ProductName);
            }
        }
    }

    private void PrepareDataColumns()
    {
        _data.Columns.Clear();
        _view.Columns.Clear();

        // add columns for the needed data to the DataTable
        _data.Columns.Add("firstname");
        _data.Columns.Add("lastname");
        _data.Columns.Add("phone");
        // add a column fo every product in your details list
        foreach ( string productName in _productNames )
        {
            _data.Columns.Add(productName);
        }
        _data.Columns.Add("TotalPrice");

        // now add the same columns to the ListView
        foreach ( DataColumn column in _data.Columns )
        {
            _view.Columns.Add(column.ColumnName);
        }
    }

    private void FillData()
    {
        // fill data table with data from your list
        foreach ( AgentSalesDetailsList detail in _details )
        {
            DataRow row = _data.NewRow();

            row["firstname"] = detail.firstname;
            row["lastname"] = detail.lastname;
            row["phone"] = detail.phone;
            row["TotalPrice"] = detail.TotalPrice;

            // init product columns with -
            foreach ( string productName in _productNames )
            {
                row[productName] = "-";
            }

            // set the product columns
            foreach ( SalesProducts product in detail.Products )
            {
                row[product.ProductName] = product.amount;
            }

            _data.Rows.Add(row);
        }
    }

    private void FillView()
    {
        // copy data from table to view
        foreach ( DataRow row in _data.Rows )
        {
            ListViewItem item = new ListViewItem(row[0].ToString());
            for ( int i = 1; i < _data.Columns.Count; i++ )
            {
                item.SubItems.Add(row[i].ToString());
            }
            _view.Items.Add(item);
        }
    }
}
公共类代理SalesDetailListViewAdapter
{
私人名单(详情);;
私有列表视图_视图;
私有ISet _PRODUCTNAME=new SortedSet();
私有数据表_data=新数据表();
公共代理LessDetailListViewAdapter(列表详细信息,ListView视图)
{
_细节=细节;
_视图=视图;
}
公共视图()
{
PopulateProductNames();
制备色谱柱();
FillData();
FillView();
}
私有void PopulateProductNames()
{
_productNames.Clear();
//将所有产品名称收集到一个集合中
foreach(代理详细信息在_详细信息中列出详细信息)
{
foreach(销售产品详细信息。产品)
{
_productNames.Add(product.ProductName);
}
}
}
私有无效预处理列()
{
_data.Columns.Clear();
_view.Columns.Clear();
//将所需数据的列添加到DataTable
_data.Columns.Add(“firstname”);
_data.Columns.Add(“lastname”);
_data.Columns.Add(“电话”);
//为详细信息列表中的每个产品添加一列
foreach(名称为“\u productName”中的字符串productName)
{
_data.Columns.Add(productName);
}
_数据。列。添加(“总价”);
//现在将相同的列添加到ListView
foreach(data.Columns中的DataColumn列)
{
_view.Columns.Add(column.ColumnName);
}
}
私有void FillData()
{
//用列表中的数据填充数据表
foreach(代理详细信息在_详细信息中列出详细信息)
{
DataRow行=_data.NewRow();
行[“firstname”]=detail.firstname;
行[“lastname”]=detail.lastname;
行[“电话”]=detail.phone;
行[“TotalPrice”]=detail.TotalPrice;
//带有-
foreach(名称为“\u productName”中的字符串productName)
{
行[productName]=“-”;
}
//设置产品列
foreach(销售产品详细信息。产品)
{
行[product.ProductName]=product.amount;
}
_data.Rows.Add(行);
}
}
私有void FillView()
{
//将数据从表复制到视图
foreach(data.Rows中的数据行)
{
ListViewItem=new ListViewItem(行[0].ToString());
对于(int i=1;i<\u data.Columns.Count;i++)
{
item.SubItems.Add(行[i].ToString());
}
_查看.项目.添加(项目);
}
}
}

现在,您只需将代理销售详细信息和ListView传递给适配器的构造函数,然后调用
SetUpView()

我建议创建一个适配器类,该类执行必要的转换,以使数据适合列表视图:

public class AgentSalesDetailListViewAdapter
{
    private List<AgentSalesDetailsList> _details;
    private ListView                    _view;
    private ISet<string>                _productNames = new SortedSet<string>();
    private DataTable                   _data         = new DataTable();

    public AgentSalesDetailListViewAdapter(List<AgentSalesDetailsList> details, ListView view)
    {
        _details = details;
        _view    = view;
    }

    public void SetUpView()
    {
        PopulateProductNames();
        PrepareDataColumns();
        FillData();
        FillView();
    }

    private void PopulateProductNames()
    {
        _productNames.Clear();

        // collect all product names into a single set
        foreach ( AgentSalesDetailsList detail in _details )
        {
            foreach ( SalesProducts product in detail.Products )
            {
                _productNames.Add(product.ProductName);
            }
        }
    }

    private void PrepareDataColumns()
    {
        _data.Columns.Clear();
        _view.Columns.Clear();

        // add columns for the needed data to the DataTable
        _data.Columns.Add("firstname");
        _data.Columns.Add("lastname");
        _data.Columns.Add("phone");
        // add a column fo every product in your details list
        foreach ( string productName in _productNames )
        {
            _data.Columns.Add(productName);
        }
        _data.Columns.Add("TotalPrice");

        // now add the same columns to the ListView
        foreach ( DataColumn column in _data.Columns )
        {
            _view.Columns.Add(column.ColumnName);
        }
    }

    private void FillData()
    {
        // fill data table with data from your list
        foreach ( AgentSalesDetailsList detail in _details )
        {
            DataRow row = _data.NewRow();

            row["firstname"] = detail.firstname;
            row["lastname"] = detail.lastname;
            row["phone"] = detail.phone;
            row["TotalPrice"] = detail.TotalPrice;

            // init product columns with -
            foreach ( string productName in _productNames )
            {
                row[productName] = "-";
            }

            // set the product columns
            foreach ( SalesProducts product in detail.Products )
            {
                row[product.ProductName] = product.amount;
            }

            _data.Rows.Add(row);
        }
    }

    private void FillView()
    {
        // copy data from table to view
        foreach ( DataRow row in _data.Rows )
        {
            ListViewItem item = new ListViewItem(row[0].ToString());
            for ( int i = 1; i < _data.Columns.Count; i++ )
            {
                item.SubItems.Add(row[i].ToString());
            }
            _view.Items.Add(item);
        }
    }
}
公共类代理SalesDetailListViewAdapter
{
私人名单(详情);;
私有列表视图_视图;
私有ISet _PRODUCTNAME=new SortedSet();
私有数据表_data=新数据表();
公共代理LessDetailListViewAdapter(列表详细信息,ListView视图)
{
_细节=细节;
_视图=视图;
}
公共视图()
{
PopulateProductNames();
制备色谱柱();
FillData();
FillView();
}
私有void PopulateProductNames()
{
_productNames.Clear();
//将所有产品名称收集到一个集合中
foreach(代理详细信息在_详细信息中列出详细信息)
{
foreach(销售产品详细信息。产品)
{
_productNames.Add(product.ProductName);
}
}
}
私有无效预处理列()
{
_data.Columns.Clear();
_view.Columns.Clear();
//将所需数据的列添加到DataTable
_data.Columns.Add(“firstname”);
_data.Columns.Add(“lastname”);
_data.Columns.Add(“电话”);
//为详细信息列表中的每个产品添加一列
foreach(名称为“\u productName”中的字符串productName)
{
_data.Columns.Add(productName);
}
_数据。列。添加(“总价”);
//现在将相同的列添加到ListView
foreach(data.Columns中的DataColumn列)
{
_view.Columns.Add(column.ColumnName);
}
}
私有void FillData()
{
//用列表中的数据填充数据表
foreach(代理详细信息在_详细信息中列出详细信息)
{
DataRow行=_data.NewRow();
行[“firstname”]=detail.firstname;
行[“lastname”]=detail.lastname;
行[“电话”]=detail.phone;
行[“TotalPrice”]=detail.TotalPrice;
//带有-
foreach(名称为“\u productName”中的字符串productName)
{
行[productName]=“-”;
}
//设置产品列
foreach(销售产品详细信息。产品)
{
行[product.ProductName]=product.amount;
}
_data.Rows.Add(行);
}
}
私有void FillView()
{
//将数据从表复制到视图
弗雷奇(
public class AgentSalesDetailListViewAdapter
{
    private List<AgentSalesDetailsList> _details;
    private ListView                    _view;
    private ISet<string>                _productNames = new SortedSet<string>();
    private DataTable                   _data         = new DataTable();

    public AgentSalesDetailListViewAdapter(List<AgentSalesDetailsList> details, ListView view)
    {
        _details = details;
        _view    = view;
    }

    public void SetUpView()
    {
        PopulateProductNames();
        PrepareDataColumns();
        FillData();
        FillView();
    }

    private void PopulateProductNames()
    {
        _productNames.Clear();

        // collect all product names into a single set
        foreach ( AgentSalesDetailsList detail in _details )
        {
            foreach ( SalesProducts product in detail.Products )
            {
                _productNames.Add(product.ProductName);
            }
        }
    }

    private void PrepareDataColumns()
    {
        _data.Columns.Clear();
        _view.Columns.Clear();

        // add columns for the needed data to the DataTable
        _data.Columns.Add("firstname");
        _data.Columns.Add("lastname");
        _data.Columns.Add("phone");
        // add a column fo every product in your details list
        foreach ( string productName in _productNames )
        {
            _data.Columns.Add(productName);
        }
        _data.Columns.Add("TotalPrice");

        // now add the same columns to the ListView
        foreach ( DataColumn column in _data.Columns )
        {
            _view.Columns.Add(column.ColumnName);
        }
    }

    private void FillData()
    {
        // fill data table with data from your list
        foreach ( AgentSalesDetailsList detail in _details )
        {
            DataRow row = _data.NewRow();

            row["firstname"] = detail.firstname;
            row["lastname"] = detail.lastname;
            row["phone"] = detail.phone;
            row["TotalPrice"] = detail.TotalPrice;

            // init product columns with -
            foreach ( string productName in _productNames )
            {
                row[productName] = "-";
            }

            // set the product columns
            foreach ( SalesProducts product in detail.Products )
            {
                row[product.ProductName] = product.amount;
            }

            _data.Rows.Add(row);
        }
    }

    private void FillView()
    {
        // copy data from table to view
        foreach ( DataRow row in _data.Rows )
        {
            ListViewItem item = new ListViewItem(row[0].ToString());
            for ( int i = 1; i < _data.Columns.Count; i++ )
            {
                item.SubItems.Add(row[i].ToString());
            }
            _view.Items.Add(item);
        }
    }
}