簡體   English   中英

我想知道,如何在按鈕單擊C#中的DataGridView列中按顏色突出顯示非數字值

[英]I want to know, how to highlight non numeric value by color in DataGridView column in button click C#

我想通過單擊C#中的按鈕來突出顯示顏色的非數字值。 我嘗試過以下代碼來獲取輸出,但是我沒有成功。

誰能幫我這個?

private void Stnineteen_Click(object sender, EventArgs e)
    {       

        for (int i = DataGridView1.RowCount - 2; i >= 0; i--)
        {
            for (int j = 0; j < DataGridView1.RowCount - 0; j++)
            {
                string grid1 = DataGridView1.Rows[i].Cells[10].Value.ToString();

                if (grid1 =="")
               {
                    DataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    //break;

                }
                else if(grid1 == "0")
                {

                    DataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    //break;
                }
                else
                {
                    DataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                }
            }


        }

這樣的事情應該有效:

double d;
foreach(DataGridViewRow r in DataGridView1.Rows)
{
    if (r.IsNewRow) return;  //we don't want to do anything to the edit row.

if (r.Cells[1].Value == null || r.Cells[10].Value.ToString() == "0")
{
    r.Cells[1].Style.BackColor = Color.Red;
}
else if(double.TryParse(r.Cells[10].Value.ToString(), out d)) //if it can be parsed to a number
{
    r.Cells[1].Style.BackColor = Color.Green;
}
else
{
    //Cannot be parsed to a number 
    r.Cells[10].Style.BackColor = Color.Yellow;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM