Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] DataGridView Sortieren

Empfohlene Antworten

Veröffentlicht

Hi,

ich habe in meinem Programm zwei DataGridViews, die als datasource über bindingsource jeweils ein anderes Objekt meines Programms haben.

Die einzelnen Zeilen landen alle bei beiden wie gewollt in der Tabelle.

Allerdings kann ich die Tabelle nicht nach einzelnen Spalten sortieren.

Also die ColumnHeaders sind nichtmals klickbar.

Habe die Beispiele aus der Hilfe getestet

(How to: Customize Sorting in the Windows Forms DataGridView Control)

und dort wird der ColumnHeader beim MouseOver ja heller, derartiges ist bei mir nicht der fall.

SelectionMode ist nicht auf ColumnHeaderSelect.

Der Sortmode der einzelnen Spalten ist stets Automatic.

Die Spalten sind immer TextBoxColumns.

Ich benutze im Übrigen Visual Stuido 2005 Beta 2

Jemand ne Idee?

Ok, ich habe mich vom Funktionierenden aus fortbewegt, und das vom Konzept her an das angepasst was ich habe.

Ich füge einmal Rows direkt hinzu, und einmal über die datasource. Welche Rows angezeigt werden hängt davon ab ob

this.dataGridView1.DataSource = this.statistic;

auskommentiert ist oder nicht. Mit der Zeile funktioniert das sortieren nicht mehr. An welcher stelle muss ich jetzt eingreifen?

Hier ist aber im gegensatz zu meinem eigentlichen Code der ColumnHeader noch klickbar, allerdings taucht das sortier icon nicht auf und dataGridView1_SortCompare wird aber nie aufgerufen (per breakpoint darin getestet).

Denkbare Lösung wäre halt Icon und Sort selber zu setzen/aufzurufen bei einem .Click auf den jeweiligen Header, möchte aber lieber wissen warum es nicht funktioniert :).

#region Using directives

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

#endregion

class Form1 : Form

{

private DataGridView dataGridView1 = new DataGridView();

private BindingSource statistic;

private DataGridViewTextBoxColumn statcolumn1 = new DataGridViewTextBoxColumn();

private DataGridViewTextBoxColumn statcolumn2 = new DataGridViewTextBoxColumn();

private DataGridViewTextBoxColumn statcolumn3 = new DataGridViewTextBoxColumn();

public Form1()

{

dataGridView1.AllowUserToAddRows = false;

dataGridView1.Dock = DockStyle.Fill;

Controls.Add(this.dataGridView1);

this.Text = "DataGridView.SortCompare demo";

}

public void PopulateDataGridView()

{

this.statcolumn1.DataPropertyName = "ID";

this.statcolumn2.DataPropertyName = "Name";

this.statcolumn3.DataPropertyName = "City";

this.statcolumn1.Name = "ID";

this.statcolumn2.Name = "Name";

this.statcolumn3.Name = "City";

this.dataGridView1.Columns.Add(this.statcolumn1);

this.dataGridView1.Columns.Add(this.statcolumn2);

this.dataGridView1.Columns.Add(this.statcolumn3);

dataGridView1.Rows.Add(new string[] { "1", "Parker", "Seattle" });

dataGridView1.Rows.Add(new string[] { "2", "Parker", "New York" });

dataGridView1.Rows.Add(new string[] { "3", "Watson", "Seattle" });

dataGridView1.Rows.Add(new string[] { "4", "Jameson", "New Jersey" });

dataGridView1.Rows.Add(new string[] { "5", "Brock", "New York" });

dataGridView1.Rows.Add(new string[] { "6", "Conner", "Portland" });

person first = new person ("1", "Parker", "Seattle");

person second = new person("2", "Parker", "New York" );

person third = new person("3", "Watson", "Seattle" );

this.statistic = new BindingSource();

this.statistic.DataSource = typeof(person);

/*

*Wird die nächste Zeile auskommentiert funkioniert das sortieren wieder, allerdings mit den per Rows.Add hinzugefügten Personen.

*/

this.dataGridView1.DataSource = this.statistic;

this.dataGridView1.SortCompare +=new DataGridViewSortCompareEventHandler(dataGridView1_SortCompare);

this.statistic.Add(first);

this.statistic.Add(second);

this.statistic.Add(third);

this.dataGridView1.AutoResizeColumns();

}

protected override void OnLoad(EventArgs e)

{

PopulateDataGridView();

base.OnLoad(e);

}

static void Main()

{

Application.EnableVisualStyles();

Application.Run(new Form1());

}

private void dataGridView1_SortCompare(object sender,

DataGridViewSortCompareEventArgs e)

{

e.SortResult = System.String.Compare(

e.CellValue1.ToString(), e.CellValue2.ToString());

if (e.SortResult == 0 && e.Column.Name != "ID")

{

e.SortResult = System.String.Compare(

dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),

dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());

}

e.Handled = true;

}

public class person

{

public string id, name, city;

public person(string id, string name, string city)

{

this.id = id;

this.name = name;

this.city = city;

}

public string Name

{

get { return name; }

}

public string ID

{

get { return id; }

}

public string City

{

get { return city; }

}

}

}

Den Code nochmal ordentlich...


#region Using directives


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;


#endregion

class Form1 : Form

{

    private DataGridView dataGridView1 = new DataGridView();

    private BindingSource statistic;

    private DataGridViewTextBoxColumn statcolumn1 = new DataGridViewTextBoxColumn();

    private DataGridViewTextBoxColumn statcolumn2 = new DataGridViewTextBoxColumn();

    private DataGridViewTextBoxColumn statcolumn3 = new DataGridViewTextBoxColumn();


    public Form1()

    {

        // Initialize the form.

        // This code can be replaced with designer generated code.

        dataGridView1.AllowUserToAddRows = false;

        dataGridView1.Dock = DockStyle.Fill;

        Controls.Add(this.dataGridView1);

        this.Text = "DataGridView.SortCompare demo";

    }


    public void PopulateDataGridView()

    {

        this.statcolumn1.DataPropertyName = "ID";

        this.statcolumn2.DataPropertyName = "Name";

        this.statcolumn3.DataPropertyName = "City";

        this.statcolumn1.Name = "ID";

        this.statcolumn2.Name = "Name";

        this.statcolumn3.Name = "City";

        this.dataGridView1.Columns.Add(this.statcolumn1);

        this.dataGridView1.Columns.Add(this.statcolumn2);

        this.dataGridView1.Columns.Add(this.statcolumn3);


        dataGridView1.Rows.Add(new string[] { "1", "Parker", "Seattle" });

        dataGridView1.Rows.Add(new string[] { "2", "Parker", "New York" });

        dataGridView1.Rows.Add(new string[] { "3", "Watson", "Seattle" });

        dataGridView1.Rows.Add(new string[] { "4", "Jameson", "New Jersey" });

        dataGridView1.Rows.Add(new string[] { "5", "Brock", "New York" });

        dataGridView1.Rows.Add(new string[] { "6", "Conner", "Portland" });


        person first = new person ("1", "Parker", "Seattle");

        person second = new person("2", "Parker", "New York" );

        person third = new person("3", "Watson", "Seattle" );


        this.statistic = new BindingSource();

        this.statistic.DataSource = typeof(person);

        this.dataGridView1.DataSource = this.statistic;

        this.dataGridView1.SortCompare +=new DataGridViewSortCompareEventHandler(dataGridView1_SortCompare);


        this.statistic.Add(first);

        this.statistic.Add(second);

        this.statistic.Add(third);

        this.dataGridView1.AutoResizeColumns();

    }


    protected override void OnLoad(EventArgs e)

    {

        PopulateDataGridView();

        base.OnLoad(e);

    }


    // Establish the main entry point for the application.

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.Run(new Form1());

    }


    private void dataGridView1_SortCompare(object sender,

        DataGridViewSortCompareEventArgs e)

    {

        // Try to sort based on the cells in the current column.

        e.SortResult = System.String.Compare(

            e.CellValue1.ToString(), e.CellValue2.ToString());


        // If the cells are equal, sort based on the ID column.

        if (e.SortResult == 0 && e.Column.Name != "ID")

        {

            e.SortResult = System.String.Compare(

                dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),

                dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());

        }

        e.Handled = true;

    }


    public class person

    {

        public string id, name, city;

        public person(string id, string name, string city)

        {

            this.id = id;

            this.name = name;

            this.city = city;

        }

        public string Name

        {

            get { return name; }

        }

        public string ID

        {

            get { return id; }

        }


        public string City

        {

            get { return city; }

        }

    }

}



Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.