Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按前文本颜色对dataGridView列排序_C#_Sorting_Datagridview - Fatal编程技术网

C# 按前文本颜色对dataGridView列排序

C# 按前文本颜色对dataGridView列排序,c#,sorting,datagridview,C#,Sorting,Datagridview,我有一个带有数据的datagridview,数据行被着色(仅文本着色),如下所示:红色、橙色和黑色 例如: private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { switch (e.Value.ToString()) { case "SDP": e.Valu

我有一个带有数据的datagridview,数据行被着色(仅文本着色),如下所示:红色、橙色和黑色 例如:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  switch (e.Value.ToString())
            {
                case "SDP":
                    e.Value = "Schedule Departure";
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                    break;
                case "CKN":
                    e.Value = "Check-In";
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Orange;
                    break;
                case "P2G":
                    e.Value = "Proceed to Gate";
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
                    break;
}
我还创建了带有几个单选按钮的Groupbox来更改排序

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        ListSortDirection direction;
        direction = ListSortDirection.Ascending;
        dataGridView1.Sort(arrTimeDataGridViewTextBoxColumn,direction);
    }

但问题是:如何根据文本的颜色更改排序,例如:红色、橙色和黑色?

datagridview
中创建一列,您可以在其中保留一些
前景色。
例如:

列将命名为
dataGridView\u ForeColorNum
,值如下:

RED = 1
ORANGE = 2
BLACK = 3
{
    if(this.datagridview1.Columns[e.ColumnIndex].Name != this.dataGridView_ForeColorNum.Name)
        return;
    //if column is ForeColumn then set a ForeColor independent on the column value
    switch ((int)e.Value)
    {
        case 1: //SDP
            e.Value = "Schedule Departure";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;     
            break;
        case 2: //SKN
            e.Value = "Check-In";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Orange;
            break;
        case 3: //P2G
            e.Value = "Proceed to gate";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
            break;
    }
}
然后在handler
dataGridView1\u CellFormatting
中,在更改行的
ForeColor
的同时设置此值

cswitch (e.Value.ToString())
{
    case "SDP":
        e.Value = "Schedule Departure";
        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
        this.dataGridView1.Rows[e.RowIndex].Cells[dataGridView_ForeColorNum.Name].Value = 1;     
        break;
    case "SKN":
        e.Value = "Check-In";
        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Orange;
        this.dataGridView1.Rows[e.RowIndex].Cells[dataGridView_ForeColorNum.Name].Value = 2;
        break;
    case "P2G":
        e.Value = "Proceed to gate";
        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
        this.dataGridView1.Rows[e.RowIndex].Cells[dataGridView_ForeColorNum.Name].Value = 3;            break;


}
排序将与正常情况一样,但用于对给定ForeColor编号的新列进行排序:

 private void radioButton1_CheckedChanged(object sender, EventArgs e)
 {
     ListSortDirection direction;
     direction = ListSortDirection.Ascending;
     dataGridView1.Sort(dataGridView_ForeColorNum,direction);
 }
对于数据绑定DataGridView,以前的解决方案将不起作用,因为预定义列的属性
IsDataBound
=False

在这种情况下,我想到的第一个解决方案是:

在SQL查询中再添加一列,您将使用
CASE WHEN
语句设置
ForeColor
编号:

CASE yourValueColumn
WHEN 'SDP' THEN 1
WHEN 'SKN' THEN 2
WHEN 'P2G' THEN 3
ELSE 0 END AS ForeColor
在datagridview的预定义列
datagridview\u ForeColorNum
设置属性
DataProperty=“ForeColor”
datagridview\u CellFormatting
处理程序如下:

RED = 1
ORANGE = 2
BLACK = 3
{
    if(this.datagridview1.Columns[e.ColumnIndex].Name != this.dataGridView_ForeColorNum.Name)
        return;
    //if column is ForeColumn then set a ForeColor independent on the column value
    switch ((int)e.Value)
    {
        case 1: //SDP
            e.Value = "Schedule Departure";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;     
            break;
        case 2: //SKN
            e.Value = "Check-In";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Orange;
            break;
        case 3: //P2G
            e.Value = "Proceed to gate";
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
            break;
    }
}

感谢您的回答,这是一个惊人的想法,但它给了我以下错误:数据绑定DataGridView控件只能在数据绑定列上排序。参数名称:dataGridViewColumn@NawafAl-米什里,我更新了我的答案。想法是在数据库级别创建一列ForeColor,然后绑定到DataGridView,然后在其中设置ForeColor并对其进行排序感谢这个解决方案,是的,我做了一些类似于微笑的事情,现在工作正常:)非常感谢你的提示,你是如此的有用,兄弟