2013-02-25 57 views
0

我是ASP.net的新手,我想以编程方式创建一个动态Listview组件。 我找到了关于如何为Gridview和Datatable做但不是Listview的例子。可能吗? 有谁知道一个很好的教程?以编程方式在asp.net中创建列表视图

+0

请发布'Gridview和Datatable'的示例链接,以便我们确切地知道你在问什么。 – Win 2013-02-25 20:08:28

回答

2

试试这个

private void CreateMyListView() 
{ 
    // Create a new ListView control. 
    ListView listView1 = new ListView(); 
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200)); 
    // Set the view to show details. 
    listView1.View = View.Details; 
    // Allow the user to edit item text. 
    listView1.LabelEdit = true; 
    // Allow the user to rearrange columns. 
    listView1.AllowColumnReorder = true; 
    // Display check boxes. 
    listView1.CheckBoxes = true; 
    // Select the item and subitems when selection is made. 
    listView1.FullRowSelect = true; 
    // Display grid lines. 
    listView1.GridLines = true; 
    // Sort the items in the list in ascending order. 
    listView1.Sorting = SortOrder.Ascending; 

    //Creat columns: 
    ColumnHeader column1 = new ColumnHeader(); 
    column1.Text = "Customer ID"; 
    column1.Width = 159; 
    column1.TextAlign = HorizontalAlignment.Left; 

    ColumnHeader column2 = new ColumnHeader(); 
    column2.Text = "Customer name"; 
    column2.Width = 202; 
    column2.TextAlign = HorizontalAlignment.Left; 

    //Add columns to the ListView: 
    listView1.Columns.Add(column1); 
    listView1.Columns.Add(column2); 


    // Add the ListView to the control collection. 
    this.Controls.Add(listView1); 
} 

还是来看看如何处理这个任务是Example

Imports System 
Imports System.Drawing 
Imports System.Windows.Forms 

Public Class listview 
Inherits Form 

Friend WithEvents btnCreate As Button 

Public Sub New() 
    Me.InitializeComponent() 
End Sub 

Private Sub InitializeComponent() 
    btnCreate = New Button 
    btnCreate.Text = "Create" 
    btnCreate.Location = New Point(10, 10) 

    Me.Controls.Add(btnCreate) 
    Text = "Countries Statistics" 
    Size = New Size(450, 245) 
    StartPosition = FormStartPosition.CenterScreen 
End Sub 

Private Sub btnCreate_Click(ByVal sender As System.Object, _ 
         ByVal e As System.EventArgs) Handles btnCreate.Click 

    Dim lvwCountries As ListView = New ListView 
    lvwCountries.Location = New Point(10, 40) 
    lvwCountries.Width = 420 
    lvwCountries.Height = 160 

    Controls.Add(lvwCountries) 

End Sub 

Public Shared Sub Main() 
    Application.Run(New Exercise) 
End Sub 

End Class 
+1

这是Windows Forms的权利?我想这个问题是围绕Web表单... – Faiz 2013-07-01 06:40:50

1

基本思路。关键概念与GridView需要的是相同的。

1)您需要在页面上某处放置ListView - 容器

2)此容器需要运行在服务器,所以您的C#代码(服务器评估)可以将ListView添加到它。您可以使用两个示例容器:Panel和带runat=server属性的标准div标签。

3)选择代码来创建ListView控件时,将称为如何。我建议你将其定义为方法,并从任何事件你想如称之为:

protected void Page_Load(object sender, EventArgs e) 
{ 
    // Call your method here so the ListView is created 
    CreateListView(); 
} 

private void CreateListView() 
{ 
    // Code to create ListView here 
} 

4)使用下面的代码在上面的方法来创建ListView并将其添加到像这样的容器:

var myListView = new ListView(); 
containerName.Controls.Add(myListView); 

你将需要添加到ListView性能它是美观,对明显的数据绑定的顶部。

this page上找到的代码有一些您最希望使用的示例属性。