0

我是 ASP.net 的新手,我想以编程方式创建一个动态 Listview 组件。我找到了有关如何为 Gridview 和 Datatable 而不是 Listview 执行此操作的示例。可能吗?有谁知道好的教程吗?

4

2 回答 2

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);
 }

或者看看那个例子

 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
于 2013-02-25T20:30:38.863 回答
1

如何处理此任务的基本思想。GridView关键概念与需要的相同。

1)您需要在页面上的某个地方放置ListView- 一个容器

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

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属性以使其美观。

此页面上的代码包含一些您很可能想要使用的示例属性。

于 2013-02-25T20:36:13.693 回答