开发者

How I can view result of method in Listview in C#

I build website with asp.net and c# .I have the following method

    public List<string> GetAllNameDoc(List<int> ids)
{
    List<string> Names = new List<string>();
    foreach (int r in ids)
        Names.Add(GetNameDoc(r));
    return Names;
}

I want to view the result开发者_运维技巧 of this method in DataList or Listview or Gridview component.MayBe the Kind of data source of component is object But the problem How can I pass the parameter "ids" into these comonents. I try

    DataList1.DataSource = GetAllNameDoc(ids);
    DataList1.DataBind();

but it is not work. thanks to every one tried help me and I hope that I explained the problem now well.


Assuming this is WPF. You can create a class that represents your document (a property for the id and a property for the document name). Then, you can populate a list of those objects, and set them as the itemssource of the listview. Below is an example.

In your Xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<ListView x:Name="TestListView">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding=
 "{Binding Path=Id}" 
 Header="DocumentId" Width="Auto"/>
<GridViewColumn DisplayMemberBinding=
 "{Binding Path=DocName}" 
 Header="DocName" Width="Auto">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Window>

In your code-behind:

namespace WpfApplication1
{

public partial class MainWindow : Window
{    
    public MainWindow()
    {
        InitializeComponent();
        List<int> ids = new List<int>() { 1, 2, 3 };
        this.TestListView.ItemsSource = GetAllNameDoc(ids);
    }

    public List<Docs> GetAllNameDoc(List<int> ids)
    {
        List<Docs> docs = new List<Docs>();
        foreach (int r in ids)
        {
            docs.Add(new Docs() { Id = r, DocName = GetNameDoc(r) });
        }
        return docs;
    }

    private string GetNameDoc(int id)
    {
        return "SomeDocName";
    }


}

public class Docs
{
    public int Id { get; set; }
    public string DocName { get; set; }
}
}


Linq might get you around the question of how to pass the id:

public List<string> GetAllNameDoc(List<int> ids) {     
  return ids.Select(id => GetNameDoc(id)).ToList(); 
}

I agree with @Hans, it's hard to be more helpful without more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜