[英]how to insert a datagridview value to database on button click in c#
[英]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.