2013-02-25 95 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上找到的代碼有一些您最希望使用的示例屬性。