This is a migrated thread and some comments may be shown as answers.

RowFormatting bug

2 Answers 145 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phi
Top achievements
Rank 1
Phi asked on 03 Nov 2010, 02:20 AM
I followed some threads here about using Row.Tag to keep track of newly added rows and changed rows via RowFormatting event. It is something like this:

private void gridView_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Tag != null)
    {
        if (e.RowElement.RowInfo.Tag.ToString() == "NEW")
            e.RowElement.ForeColor = Color.Green;
        else
            e.RowElement.ForeColor = Color.Red; //Changed
    }
}

Let say I updated one row. The row is now colored in red color. When I scroll down the grid using the scroll bar, I noticed that there are more red colored rows appeared while I was scrolling down. If I stopped it quickly enough, an incorrectly red colored row would show up inside the grid's view port.

Look like somebody forgot to clear out the RowInfo information :)!

Phi

2 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 03 Nov 2010, 08:17 AM
Hello Phi,

For this i would suggest a different thing, just add a boolean column and set its width and maxwidth to 1, and just use that to check for the state of the row.

Also, about resetting the properties i would suggest ResetValue,
Please take a look at the following example:
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    public Form1()
    {
        InitializeComponent();
        LoadGrid();
    }
 
    private void LoadGrid()
    {
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);
        var checkBoxColumn = new GridViewCheckBoxColumn("Modified");
        checkBoxColumn.Width = 1;
        checkBoxColumn.MaxWidth = 1;
        radGridView1.Columns.Add(checkBoxColumn);
 
        radGridView1.CellValueChanged += new GridViewCellEventHandler(radGridView1_CellValueChanged);
        radGridView1.DataSource = new IdValuesCollection(100);
    }
 
    void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
    {
        e.Row.Cells["Modified"].Value = true;
    }
 
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        var value = e.RowElement.RowInfo.Cells["Modified"].Value;
        if (value != null && value.Equals(true))
        {
            e.RowElement.ForeColor = Color.Red;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }
}
 
public class IdValue
{
    public int Id
    {
        get;
        set;
    }
 
    public string IdName
    {
        get;
        set;
    }
 
    public override string ToString()
    {
        return IdName;
    }
}
 
public class IdValuesCollection : BindingList<IdValue>
{
    public IdValuesCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new IdValue
            {
                Id = i,
                IdName = "IdName" + i
            });
        }
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Phi
Top achievements
Rank 1
answered on 03 Nov 2010, 05:46 PM
Man! That's a lot of work for just highlight rows!!! I know that in 2010 Q2 release, the grid was updated a lot but it seems like there is a lot more work ahead! I think I will just turn off the row coloring right now and wait until there is a better way! Luckily, this is just my app so I could do that :)

Thanks,

Phi
Tags
GridView
Asked by
Phi
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Phi
Top achievements
Rank 1
Share this question
or