is it possible to reorder the listView items based on the BackColor of each row I want to keep all red rows on top regardless of its values.
is it possible to reorder the listView items based on the BackColor of each row I want to keep all red rows on top regardless of its values.
how to reorder listView basied on BackColor?
fabianus
You will find that in setting a color you have to set a value to red (0-255), green (0-255), and blue (0-255) given you are using (24 or 32-bit) true color and not a color palette.
Your listview should provide you a place to store custom information. At this location you could store an indexed value that maps to an RGB color selection. You could assign these custom colors to an array of your own creation or to a color palette. Then store the array or palette index of the color in the custom store location provided to each item-cell in the listview. This you can use this index to custom sort the listview by color.
You will need to subclass the listview to add an event to trigger the sort. This event could be triggered by a user-defined action or automatically such as when the WM_ERASEBACKGROUND callback event is triggered.
James
Dallas, TX
Chips_in
it might be a sily solution but you can try this.
make the first colument of your list view a sorting column that is add only numbers in it. for example when you make the background color of a row to red make the first column property say '0'. so when you are done all coulumns having as background color of red shoud have the value 0 on their first columns.
after that just sort the list view using this code
ListView1.Sorting = SortOrder.Ascending;
ListView1.Sort();
here the sample i tried
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
internal System.Windows.Forms.ListView ListView1;
internal System.Windows.Forms.ColumnHeader ColumnHeader1;
internal System.Windows.Forms.ColumnHeader ColumnHeader2;
internal System.Windows.Forms.ColumnHeader ColumnHeader3;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// 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()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
"1",
"Normal",
"22356"}, -1);
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {
"0",
"Red",
"123456"}, -1, System.Drawing.Color.Empty, System.Drawing.Color.IndianRed, null);
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {
"1",
"Normal",
"22332"}, -1);
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem(new string[] {
"0",
"Red",
"688864"}, -1, System.Drawing.Color.Empty, System.Drawing.Color.IndianRed, null);
this.ListView1 = new System.Windows.Forms.ListView();
this.ColumnHeader1 = new System.Windows.Forms.ColumnHeader();
this.ColumnHeader2 = new System.Windows.Forms.ColumnHeader();
this.ColumnHeader3 = new System.Windows.Forms.ColumnHeader();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ListView1
//
this.ListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.ColumnHeader1,
this.ColumnHeader2,
this.ColumnHeader3});
this.ListView1.GridLines = true;
this.ListView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4});
this.ListView1.Location = new System.Drawing.Point(8, 8);
this.ListView1.Name = "ListView1";
this.ListView1.Size = new System.Drawing.Size(184, 240);
this.ListView1.TabIndex = 1;
this.ListView1.View = System.Windows.Forms.View.Details;
//
// ColumnHeader1
//
this.ColumnHeader1.Text = "Sort";
//
// ColumnHeader2
//
this.ColumnHeader2.Text = "Color";
//
// ColumnHeader3
//
this.ColumnHeader3.Text = "Value";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 264);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(192, 325);
this.Controls.Add(this.button1);
this.Controls.Add(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 button1_Click(object sender, System.EventArgs e)
{
ListView1.Sorting = SortOrder.Ascending;
ListView1.Sort();
}
}
}
Kees_de_Waard
Below is a sample from msdn I modified slightly. You can modify it to work how you need it based on items or subitems but this should help some. Look here for more info to http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemsorter.aspx
// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].BackColor.ToString(), ((ListViewItem)y).SubItems[col].BackColor.ToString());
}
}
Spenceee
This is a good alternative given the ListView has built-in support for sorting on the first column; in your case, this fist column of course should be made hidden to the user. You can extend your idea an write a little more code to custom sort on any column in the ListView if need be; and this alternate approach will then require an added event. I was looking originally at the extra bytes storage provided to a ListBox, ListView, and TreeView because it is already hidden. Anyway, your path does look like a valid answer to me and potential solution.
James