-3

我在 datagridview 中有 4 列:HomeTeam、HomeScore、AwayScore 和 AwayTeam。

我想比较 HomeScore 和 AwayScore 列。如果 HomeScore 大于 Away Score,则 HomeTeam 必须加粗。请帮忙。

4

2 回答 2

0

这将做:

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("HomeScore");
            dt.Columns.Add("AwayScore");
            dt.Columns.Add("AwayTeam");
            dt.Rows.Add(3, 1,"Everton");

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.CellFormatting +=
                new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
        }

        void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex == dataGridView1.Columns["AwayTeam"].Index)
            {
                e.FormattingApplied = true;
                int home = Convert.ToInt32(dataGridView1["HomeScore", e.RowIndex].Value);
                int away = Convert.ToInt32(dataGridView1["AwayScore", e.RowIndex].Value);
                if (home > away)
                {
                    DataGridViewCellStyle style = new DataGridViewCellStyle();

                    style.Font = new Font(dataGridView1.Font, FontStyle.Bold);
                    e.CellStyle = style;

                }
            }
        }

这样,每次您更新 homescore 或 awayscore 时,客队单元格也会更新。

于 2013-10-29T17:04:20.117 回答
0

尝试这个:

        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

        for (int i = 0; i==dataGridView1.Rows.Count; i++ )
            if ((int)dataGridView1.Rows[i].Cells[0].Value > (int)dataGridView1.Rows[i].Cells[3].Value)
        {
            dataGridView1.Rows[i].Cells[0].Style = style;
        }
        else if ((int)dataGridView1.Rows[i].Cells[0].Value < (int)dataGridView1.Rows[i].Cells[3].Value)
        {

            dataGridView1.Rows[i].Cells[3].Style = style;
        }
于 2013-10-29T16:52:46.647 回答