user2108634
user2108634

Reputation: 1

Create Listview programmatically in asp.net

I am new to ASP.net and I would like to programmatically create a dynamic Listview component. I found examples on how to do that for Gridview and Datatable but not Listview. Is it possible? Does anyone know of a good tutorial?

Upvotes: 0

Views: 7712

Answers (2)

TODOName
TODOName

Reputation: 138

Basic idea of how to approach this task. The key concepts are the same ones a GridView would require.

1) You need somewhere on the page to put the ListView - a container for it.

2) This container needs to run at the server, so your C# code (which the server evaluates) can add the ListView to it. Two example containers you could use: a Panel and a standard div tag with the runat=server property.

3) Choose when the code to create the ListView will be called and how. I recommend you define it as a method and call it from whatever event you want e.g.:

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) Use below code in above method to create the ListView and add it to the container like so:

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

You will need to add to the ListView's properties for it to be aesthetically pleasing, on top of the obvious databinding.

The code found on this page has some example properties you will most likely want to use.

Upvotes: 1

syed mohsin
syed mohsin

Reputation: 2938

Try this

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

Or take a look at that 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

Upvotes: 2

Related Questions