-
Listview??
Hi, I've got a stupid problem:
I want to show the column header in a listview, but I really can't figure out what is wrong with it. I can't work it though
here is the simple code, only one listview on the form:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace listview
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
listview();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(200, 48);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(432, 144);
this.listView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 365);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listView1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listview()
{
ListView listView1 = new ListView();
listView1.Columns.Add("column",-2, HorizontalAlignment.Left);
}
}
}
Please help me, thanx
-
Please use code tags next time.
You already have a listview. Make sure in the designer that the style of the listview is "Report" or something like that. Try each style until you see Columns. You can add columns and data with the designer. If you want to do it with code, you have to use the listview of the form you already made with the designer:
ListView listView1 = new ListView();
This line creates a local variable listView1 and thereby hides your listView1 of your form. The ListView is already there, no need to recreate it.
listView1.Columns.Add("column",-2, HorizontalAlignment.Left);
This should work.
-
thank you,nvoigt!
I found that the item in listview is uneditable, is there any example or article about this problem?
thanx!!
-
It's editable in code and in the designer. If you need it to be editable by users, maybe a grid is the better choice.
-
thanx again, another problem about listview property. if there are some columns in the listview and I want to set the column size fixed, which property is it?