C# DataGridView比较结果

C# DataGridView比较结果,c#,datagridview,C#,Datagridview,我正在使用DataGridView,以便将一些评分结果放在那里。但我应该将结果和上周的结果进行比较,并将其设置为绿色或红色。。。。如果可能,在值前面添加差值 例如1400+10 在这个示例中,用户得到了1400分,比上周多了10分 以前有人试过这个吗 谢谢使用DataGridView的事件设置背景色和要显示的自定义文本 当事件触发显示本周分数的列中的单元格时,您将进行格式化。尝试将此值和上周分数(在同一行的另一列中)的值转换为ints,然后进行比较。如果差异不为零,请使用事件的CellStyle

我正在使用DataGridView,以便将一些评分结果放在那里。但我应该将结果和上周的结果进行比较,并将其设置为绿色或红色。。。。如果可能,在值前面添加差值

例如1400+10

在这个示例中,用户得到了1400分,比上周多了10分

以前有人试过这个吗

谢谢

使用DataGridView的事件设置背景色和要显示的自定义文本

当事件触发显示本周分数的列中的单元格时,您将进行格式化。尝试将此值和上周分数(在同一行的另一列中)的值转换为
int
s,然后进行比较。如果差异不为零,请使用事件的
CellStyle
Value
属性自定义单元格的外观

像这样:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (e.ColumnIndex == 1) {
        string lastScoreString = dataGridView1.Rows[e.RowIndex].Cells[0].Value as string;
        int lastScore;
        if (int.TryParse(lastScoreString, out lastScore)) {
            string thisScoreString = dataGridView1.Rows[e.RowIndex].Cells[1].Value as string;
            int thisScore;
            if (int.TryParse(thisScoreString, out thisScore)) {
                var scoreDifference = thisScore - lastScore;
                var formattedScoreText = string.Format("{0}   {1}", thisScore, scoreDifference.ToString("+#;-#;0"));
                if (scoreDifference > 0) {
                    e.CellStyle.BackColor = Color.Green;
                    e.CellStyle.ForeColor = Color.White;  // <-- Me expressing my artistic self.
                    e.Value = formattedScoreText;
                } else if (scoreDifference < 0) {
                    e.CellStyle.BackColor = Color.Red;
                    e.Value = formattedScoreText;
                }
            }
        } else {
            //TODO Can't parse this week score.
        }
    } else {
        //TODO Can't parse last week score.
    }
}
private void dataGridView1\u单元格格式(对象发送方,DataGridViewCellFormattingEventArgs e){
如果(e.ColumnIndex==1){
string lastScoreString=dataGridView1。行[e.RowIndex]。单元格[0]。值为字符串;
最后得分;
if(int.TryParse(lastScoreString,out lastScore)){
string thisScoreString=dataGridView1。行[e.RowIndex]。单元格[1]。值为字符串;
整型得分;
if(int.TryParse(thiscorestring,out thiscore)){
var分数差=此分数-上次分数;
var formattedScoreText=string.Format(“{0}{1}”),thisScore,scoreDifference.ToString(“+#-#0”);
如果(分数差>0){
e、 CellStyle.BackColor=Color.Green;

e、 CellStyle.ForeColor=Color.White;//是的,我有。但是你的问题是什么?不要将数据放在GrdView中。将其存储为类列表,然后使用GridView显示它。