ListView Spalten sortieren!

Hier der Code:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient; //MySQL datenbank

namespace rechnungspruefung_v1_060807
{
    public partial class Form1 : Form
    {

        public Form1(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            #region MySQL-Connection -----------ÖFFNEN-----------------
            InitializeComponent();
            string connectionString =
                        "Server=172.16.0.13;" +
                        "Database=tmobile_spielwiese;" +
                        "Port=3306;" +
                        "User ID=michael;" +
                        "Password=xxxxxxxxxxx;" +
                        "Pooling=false";
            IDbConnection dbconn;
            dbconn = new MySqlConnection(connectionString);
            dbconn.Open();
            IDbCommand dbcmd = dbconn.CreateCommand();

            string query = "select tmobile_rechnung20070706.rufnummer, tmobile_rechnung20070706.grundentgelt + tmobile_rechnung20070706.verbindungsentgelte + tmobile_rechnung20070706.sonstige_entgelte + tmobile_rechnung20070706.gutschriften as entgelte_tma, tmobile_egn20070706.tarif AS tma_tarif, services_06_07.tarif as dialog_tarif FROM tmobile_rechnung20070706 LEFT JOIN tmobile_egn20070706 ON (tmobile_rechnung20070706.rufnummer = tmobile_egn20070706.rufnummer) LEFT JOIN services_06_07 ON (tmobile_rechnung20070706.rufnummer=services_06_07.anummer)";
            
            dbcmd.CommandText = query;
            
            IDataReader reader = dbcmd.ExecuteReader();
            #endregion
            
            #region Ausgabe ---------------------------------------
            while (reader.Read())
            {
                string rufnummer = (string)reader["rufnummer"];
                string tma_tarif = (string)reader["tma_tarif"];
                string entgelte_tma = Convert.ToString((double)reader["entgelte_tma"]);
                string dialog_tarif = "not found";
                if (reader.IsDBNull(3) != true) 
                {
                    dialog_tarif = (string)reader["dialog_tarif"];
                }
                string entgelte_dialog = "";

                #region TARIF - OK  

                 string tarif_ok = "!NOT OK!";
               
                if (tma_tarif == "Wholesale Mobile Internet S" & dialog_tarif == "Mobile Internet Small")
                { 
                    tarif_ok = "OK";
                }
                else
                {
                    if (tma_tarif == "Wholesale Mobile Internet" & dialog_tarif == "Mobile Internet Mobil")
                    {
                        tarif_ok = "OK";
                    }
                    else
                    {
                        if (tma_tarif == "Wholesale Mobile Internet XL" & dialog_tarif == "Mobile Internet XL")
                        {
                            tarif_ok = "OK";
                        }
                        else
                        {
                            if (tma_tarif == "Wholesale Mobile Internet XL" & dialog_tarif == "Mobile Internet XL")
                            {
                                tarif_ok = "OK";
                            }
                            else
                            {
                                if (tma_tarif == "Mobile Internet Medium" & dialog_tarif == "Mobile Internet Mobil")
                                {
                                    tarif_ok = "OK";
                                }
                                else
                                {
                                    if (tma_tarif == "Gutschrift Wholesale Mobile Internet" & dialog_tarif == "Mobile Internet Mobil")
                                    {
                                        tarif_ok = "Gutschrift";
                                    }
                                    else
                                    {
                                        if (tma_tarif == "Rabatt auf Grundgebühr" & dialog_tarif == "Mobile Internet Mobil")
                                        {
                                            tarif_ok = "Rabatt auf Grundgebühr";
                                        }
                                        else 
                                        {
                                            if (tma_tarif == "Rabatt auf Grundgebühr" & dialog_tarif == "Mobile Internet Small")
                                            {
                                                tarif_ok = "Rabatt auf Grundgebühr";
                                            }
                                            else
                                            {
                                                if (tma_tarif == "Rabatt auf Grundgebühr" & dialog_tarif == "Mobile Internet XL")
                                                {
                                                    tarif_ok = "Rabatt auf Grundgebühr";
                                                }
                                                else
                                                {
                                                    if (tma_tarif == "Mobile Internet Small" & dialog_tarif == "Mobile Internet Small")
                                                    {
                                                        tarif_ok = "OK";
                                                    }
                                                    else
                                                    {
                                                        if (tma_tarif == "Gutschrift Wholesale Mobile Internet XL" & dialog_tarif == "Mobile Internet Mobil")
                                                        {
                                                            tarif_ok = "Gutschrift";
                                                        }
                                                        else
                                                        {
                                                            if (tma_tarif == "Gutschrift Wholesale Mobile Internet XL" & dialog_tarif == "Mobile Internet Small")
                                                            {
                                                                tarif_ok = "Gutschrift";
                                                            }
                                                            else    
                                                            {
                                                                if (tma_tarif == "Gutschrift Wholesale Mobile Internet XL" & dialog_tarif == "Mobile Internet XL")
                                                                {
                                                                    tarif_ok = "Gutschrift";
                                                                }
                                                                else
                                                                {
                                                                    if (tma_tarif == "Wholesale Mobile Internet" & dialog_tarif == "Mobile Internet Small")
                                                                    {
                                                                    tarif_ok = "OK";
                                                                    }
                                                                    else
                                                                    {
                                                                        if (tma_tarif == "Gutschrift Wholesale Mobile Internet S" & dialog_tarif == "Mobile Internet Small")
                                                                        {
                                                                            tarif_ok = "Gutschrift";
                                                                        }
                                                                        else
                                                                        {
                                                                            if (tma_tarif == "Gutschrift Wholesale Mobile Internet" & dialog_tarif == "Mobile Internet Small")
                                                                            {
                                                                                tarif_ok = "Gutschrift";
                                                                            }
                                                                            else
                                                                            {
                                                                                if (tma_tarif == "Gutschrift Wholesale Mobile Internet S" & dialog_tarif == "Mobile Internet Mobil")
                                                                                {
                                                                                    tarif_ok = "Gutschrift";
                                                                                }
                                                                                else
                                                                                {

                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // TARIF - OK  ---------------------------------------------------------------------------------------
                #endregion
                ListViewItem x = new ListViewItem(new string[] { rufnummer, tma_tarif, "€ "+entgelte_tma, dialog_tarif, entgelte_dialog, tarif_ok });

                if (tarif_ok == "!NOT OK!")
                {
                    x.BackColor = Color.Red;
                    x.ForeColor = Color.White;
                    
                    lv_ausgabe.Items.Add(x);
                    lv_ausgabe.Sort();

                }
                ListViewItemComparer sorter = new ListViewItemComparer(e.Column);
                lv_ausgabe.ListViewItemSorter = sorter;
                lv_ausgabe.Sort();

            }
            #endregion
            
            #region MySQL-Connection -----------SCHLIESSEN-----------------
            reader.Close();
            reader = null;
            dbcmd.Dispose();
            dbcmd = null;
            dbconn.Close();
            dbconn = null;
            #endregion
           
        }
        private void Lv_ausgabe_ColumnClick(object sender,
      System.Windows.Forms.ColumnClickEventArgs e, System.Windows.Forms.ListView lv_ausgabe)
        {

            ListViewItemComparer sorter = new ListViewItemComparer(e.Column);
            lv_ausgabe.ListViewItemSorter = sorter;
            lv_ausgabe.Sort();
        }
        private void Form1_Load(object sender, EventArgs e) { }
    }
}
public class ListViewItemComparer : IComparer
{

    private int column;
    private bool numeric = false;

    public int Column
    {

        get { return column; }
        set { column = value; }
    }

    public bool Numeric
    {

        get { return numeric; }
        set { numeric = value; }
    }

    public ListViewItemComparer(int columnIndex)
    {

        Column = columnIndex;
    }

    public int Compare(object x, object y)
    {

        ListViewItem listX = (ListViewItem)x;
        ListViewItem listY = (ListViewItem)y;

        if (Numeric)
        {

            // Convert column text to numbers before comparing.
            // If the conversion fails, just use the value 0.
            decimal listXVal, listYVal;
            try
            {
                listXVal = Decimal.Parse(listX.SubItems[Column].Text);
            }
            catch
            {
                listXVal = 0;
            }

            try
            {
                listYVal = Decimal.Parse(listY.SubItems[Column].Text);
            }
            catch
            {
                listYVal = 0;
            }

            return Decimal.Compare(listXVal, listYVal);
        }
        else
        {

            // Keep the column text in its native string format
            // and perform an alphabetic comparison.
            string listXText = listX.SubItems[Column].Text;
            string listYText = listY.SubItems[Column].Text;

            return String.Compare(listXText, listYText);
        }
    }
}

Meine Fehlermeldung: Keine Überladung für die Form1-Methode nimmt 0 Argumente an. in Program.cs
 
Also, den Konstruktor solltest du nicht als EventHandler missbrauchen. Dafür hast du ja private void Lv_ausgabe_ColumnClick. Hier solltest du deine Funktionalität hineinpacken.
 
habs jetzt hineingeopackt funkt aber trotzdem nicht:


Code:
Keine Überladung für die Form1-Methode nimmt 0 Argumente an.

Ich bin am verzweifeln =(
 
so code weist keine fehler auf nur das sortieren funktioniert immer noch nicht hab mich genau an die Anleitung von (http://support.microsoft.com/kb/319401) gehalten, doch es funktioniert nicht =(

Code:
private void ColumnClick(object o, ColumnClickEventArgs e)
        {
            if ( e.Column == lvwColumnSorter.SortColumn )
            {
	            // Reverse the current sort direction for this column.
	            if (lvwColumnSorter.Order == SortOrder.Ascending)
	            {
		            lvwColumnSorter.Order = SortOrder.Descending;
	            }
	            else
	            {
		            lvwColumnSorter.Order = SortOrder.Ascending;
	            }
            }
            else
            {
	            // Set the column number that is to be sorted; default to ascending.
	            lvwColumnSorter.SortColumn = e.Column;
	            lvwColumnSorter.Order = SortOrder.Ascending;
            }
            this.lv_ausgabe.Sort();

Code:
namespace rechnungspruefung_v1_060807
{
    public partial class Form1 : Form
    {
        string pfad_1;
        string pfad_2;
        private ListViewColumnSorter lvwColumnSorter; ........

Hab auch eine neue Klasse erstellt: "ListViewColumnSorter.cs":
Code:
using System.Collections;
using System.Windows.Forms;

/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
    /// <summary>
    /// Specifies the column to be sorted
    /// </summary>
    private int ColumnToSort;
    /// <summary>
    /// Specifies the order in which to sort (i.e. 'Ascending').
    /// </summary>
    private SortOrder OrderOfSort;
    /// <summary>
    /// Case insensitive comparer object
    /// </summary>
    private CaseInsensitiveComparer ObjectCompare;

    /// <summary>
    /// Class constructor.  Initializes various elements
    /// </summary>
    public ListViewColumnSorter()
    {
        // Initialize the column to '0'
        ColumnToSort = 0;

        // Initialize the sort order to 'none'
        OrderOfSort = SortOrder.None;

        // Initialize the CaseInsensitiveComparer object
        ObjectCompare = new CaseInsensitiveComparer();
    }

    /// <summary>
    /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
    /// </summary>
    /// <param name="x">First object to be compared</param>
    /// <param name="y">Second object to be compared</param>
    /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
    public int Compare(object x, object y)
    {
        int compareResult;
        ListViewItem listviewX, listviewY;

        // Cast the objects to be compared to ListViewItem objects
        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        // Compare the two items
        compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

        // Calculate correct return value based on object comparison
        if (OrderOfSort == SortOrder.Ascending)
        {
            // Ascending sort is selected, return normal result of compare operation
            return compareResult;
        }
        else if (OrderOfSort == SortOrder.Descending)
        {
            // Descending sort is selected, return negative result of compare operation
            return (-compareResult);
        }
        else
        {
            // Return '0' to indicate they are equal
            return 0;
        }
    }

    /// <summary>
    /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
    /// </summary>
    public int SortColumn
    {
        set
        {
            ColumnToSort = value;
        }
        get
        {
            return ColumnToSort;
        }
    }

    /// <summary>
    /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
    /// </summary>
    public SortOrder Order
    {
        set
        {
            OrderOfSort = value;
        }
        get
        {
            return OrderOfSort;
        }
    }

}

ich hoff i wisst was ich falsch mach =(
 
Zurück