0

我有一个带有数据的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);
    }

但问题是:如何根据文本的颜色更改排序,例如:RED, ORANGE then Black?

4

1 回答 1

1

在您的列中创建一个datagridview可以保留多个ForeColor. 例如 :

列将被命名为dataGridView_ForeColorNum,值将如下所示:

RED = 1
ORANGE = 2
BLACK = 3

然后在处理程序中dataGridView1_CellFormatting设置此值同时更改ForeColor行的 a

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。MSDN:datagridview.Sort

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

在您的 SQL 查询中再添加一列,您将在其中ForeColor使用CASE WHEN语句设置数字:

CASE yourValueColumn
WHEN 'SDP' THEN 1
WHEN 'SKN' THEN 2
WHEN 'P2G' THEN 3
ELSE 0 END AS ForeColor

在 datagridview 的预定义列dataGridView_ForeColorNum集属性DataProperty = "ForeColor"datagridview_CellFormatting处理程序将如下:

{
    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;
    }
}
于 2013-04-14T10:43:34.663 回答