| |

VerySource

 Forgot password?
 Register
Search
View: 1648|Reply: 13

How to achieve datagridview row color

[Copy link]

3

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-11 21:00:02
| Show all posts |Read mode
Fields in the table

status
0
1

How to make rows with status 0 display blue and 1 display red
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-8 08:30:01
| Show all posts
If data binding is used, the RowDataBound event of datagridview can be triggered
protected void GridViewTransportPapers_RowDataBound (object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int i = e.Row.DataItem as int; // This depends on your situation
            if (i == null)
            {
                return;
            }
            if (i == 0)
            {
                e.Row.BackColor = Color.Red;
            }
            else
            {
                e.Row.BackColor = Color.Blue;
            }
        }
    }
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-4-8 14:00:01
| Show all posts
Rewrite the onpaint function of datagird, and use dataGridTableStyle. Roughly as follows
public class DataGridColoredTextBoxColumn: DataGridTextBoxColumn
{
public DataView dv = null;

protected override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
try
{
switch (dv [rowNum] .Row [3] .ToString ())
{
case "critical": backBrush = Brushes.Red; break;
case "major": backBrush = Brushes.Red; break;
case "minor": backBrush = Brushes.Orange; break;
case "warning": backBrush = Brushes.Orange; break;
default: break;
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
finally
{
base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}
Use this class instead of DataGridTextBoxColumn to define variables: as follows
private DataGridColoredTextBoxColumn IDColumn;
That's it.
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-8 19:30:01
| Show all posts
. . . . . . I was wrong, mine was on asp.net. . .
Did not see clearly
Reply

Use magic Report

3

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-4-30 20:00:01
| Show all posts
For WINFORM, the method ofooloifewas not tried out!
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-3 10:15:01
| Show all posts
Not done yet?
Trigger the DataBindingComplete event
private void dataGridView1_DataBindingComplete (object sender, DataGridViewBindingCompleteEventArgs e)
{
    // traverse each line
    foreach (DataGridViewRow dgv in dataGridViewOutMacs.Rows)
    {
        // Take the value of a specific column, the column index is INDEX
        int i = dgv.Cells [INDEX] .Value as int;
        if (i == null)
        {
            continue; // The conversion is unsuccessful
        }
        if (i == 0)
        {
            dgv.DefaultCellStyle.BackColor = Color.Blue;
        }
        else if (i == 1)
        {
            dgv.DefaultCellStyle.BackColor = Color.Red;
        }
    }
}
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-3 11:15:01
| Show all posts
Not done yet?
Trigger the DataBindingComplete event
private void dataGridView1_DataBindingComplete (object sender, DataGridViewBindingCompleteEventArgs e)
{
    // traverse each line
    foreach (DataGridViewRow dgv in dataGridView1.Rows)
    {
        // Take the value of a specific column, the column index is INDEX
        int i = dgv.Cells [INDEX] .Value as int;
        if (i == null)
        {
            continue; // The conversion is unsuccessful
        }
        if (i == 0)
        {
            dgv.DefaultCellStyle.BackColor = Color.Blue;
        }
        else if (i == 1)
        {
            dgv.DefaultCellStyle.BackColor = Color.Red;
        }
    }
}
Reply

Use magic Report

3

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-5-4 10:00:02
| Show all posts
Why didn't I find the DataBindingComplete event?
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-7 10:30:02
| Show all posts
kyoetsuu
 
 
   Why didn't I find the DataBindingComplete event?
  
 
===============================================
It is easy to find in the "Data" events of the DataGridView, or sort the events alphabetically.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Korea, Republic of

Post time: 2020-5-7 20:00:01
| Show all posts
I really didn't. I didn't find it either! I also asked. How to change the background color of the selected row .. The default blue color of the system is not good!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list