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

Formatting on a column's value change

7 Answers 106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philippe
Top achievements
Rank 2
Philippe asked on 17 Dec 2012, 07:47 PM
Hello

In a Gridview, is there a way to use the same kind of tool than ".IsOdd", but applied to a particular cell's data? In short, I want the row color to change everytime the value in a particular column changes. Said columns happens to be my "ORDER BY" column.

private void radGridUsagers_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    e.CellElement.DrawFill = true;
    e.CellElement.GradientStyle = GradientStyles.Solid;
    if (e.CellElement.ColumnInfo.ReadOnly)
    {
        if (e.CellElement.RowElement.IsOdd)
            e.CellElement.BackColor = clsColor.ReadOnlyOdd();
        else
            e.CellElement.BackColor = clsColor.ReadOnlyEven();
     }
     else
     {
         if (e.CellElement.RowElement.IsOdd)
             e.CellElement.BackColor = clsColor.WritableOdd();
         else
             e.CellElement.BackColor = clsColor.WritableEven();
     }
}

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 20 Dec 2012, 02:17 PM
Hi Philippe,

Thank you for writing.

The provided information was not enough for me to determine what is your goal. Could you please elaborate on this and provide us with very detailed explanation on what you have now and what you want to add as functionality. Sample project, screen shots and/or video might also help.

Thank you in advance for your time and cooperation.

I am looking forward to your reply.

Greetings,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Philippe
Top achievements
Rank 2
answered on 20 Dec 2012, 02:44 PM
Hello Plamen

In the attached screenshot, you can see my current RadGridView. If you notice the column "MRDTE", it contains a YYYYMMDD type of string, representing a date. I would like to be able to change the color of the rows everytime this value changes, so that the rows are visually color-grouped.

Since the GridView is very long, the grid's loading time just got unbearable when I tried adding a column to hold a flag (column "DtOrder" in the following code snipplet), and then looped row-by-row to see if the current value had changed compared to the previous value, and set the flag accordingly.

I was wondering if there was another method, possibly faster, to achieve this result. Take note that I am already using the row's TAG value when the user modifies the row's value.

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.RowIndex > -1)
    {
        e.CellElement.DrawFill = true;
        e.CellElement.GradientStyle = GradientStyles.Linear;
        e.CellElement.NumberOfColors = 1;
        if ((string)e.Row.Tag == "Modif>") // Si il y a une modification, marquer la ligne
        {
            e.CellElement.NumberOfColors = 2;
            e.CellElement.BackColor = clsColor.ModifColor1();
            e.CellElement.BackColor2 = clsColor.ModifColor2();
         }
        else if (e.CellElement.ColumnInfo.ReadOnly)
        {
            if ((e.CellElement.RowInfo.Cells["DtOrder"].Value.ToString()) == "0")
                e.CellElement.BackColor = clsColor.ReadOnlyOdd();
            else
                e.CellElement.BackColor = clsColor.ReadOnlyEven();
        }
        else
        {
            if ((e.CellElement.RowInfo.Cells["DtOrder"].Value.ToString()) == "0")
                e.CellElement.BackColor = clsColor.WritableOdd();
            else
                e.CellElement.BackColor = clsColor.WritableEven();
        }
    }
}

And the creation and loading of the "DtOrder" column itself.
public void LoadDatagridQuery()
{
    clsDBConn.RemplirGridView(radGridView1, clsDBConn.GetCaissesForcee(), false);
    radGridView1.EnableAlternatingRowColor = true;
 
     // Mettre toutes les colonnes Read-only, sauf les caisses
    foreach (GridViewDataColumn column in this.radGridView1.Columns)
    {
        column.ReadOnly = (column.Name != "CS");
        //if (column.Name == "ID") column.IsVisible = false; //Cacher la colonne ID
        if (column.Name == "DtOrder") column.IsVisible = false; //Cacher la colonne
        if (column.Name == "CS") column.Width = column.Width * 2; //Agrandir les Caisses
    }
 
    string dtLastTime = "";
    bool curVal = true;
    foreach (GridViewRowInfo row in radGridView1.Rows)
    {
        if (row.Cells["MRDTE"].Value.ToString() == dtLastTime)
        {
            row.Cells["DtOrder"].Value = curVal ? 0 : 1;
        }
        else
        {
            dtLastTime = row.Cells["MRDTE"].Value.ToString();
            curVal = !curVal;
            row.Cells["DtOrder"].Value = curVal ? 0 : 1;
        }
    }
}
0
Accepted
Plamen
Telerik team
answered on 21 Dec 2012, 03:28 PM
Hello Philippe,

Thank you for the additional information.

What I understand is that you want to switch two colors of the rows when the value in the first column changes i.e. in your screen shot all rows with value 20121216 in the first column should be one color (let's say red) and the rest (20121223) should be another color (let's say blue). Then when the value of this cell changes again the color should be red again and so on. If so, the approach taken is the right way to go to achieve your scenario.

Could you please let me know how do you populate your grid with data? Are you binding to a DataTable? How many records your grid holds?

Alternative solution that I can offer is to use the grouping functionality of RadGridView. More information about it can be found here: http://www.telerik.com/help/winforms/gridview-grouping-setting-groups-programmatically.html.

I hope that you find this information useful. I am looking forward to your reply.

Greetings,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Philippe
Top achievements
Rank 2
answered on 21 Dec 2012, 03:43 PM
Yes, what you describe is the desired result I want to achive. The RadGridView will hold anywhere from 50k to 100k rows, with around 17 columns.

The RadGridView is populated in the "RemplirGridView" function, with a simple SQL SELECT statement returning a Datatable that is then used as the grid's datasource. I then override the "DtOrder" column (part of the SELECT statement, but only contains a static "0") with the correct values, but it is that loop that is taking too long, given the number of records.

I guess the fastest way would be to try to get my "DtOrder" flag changing in the SQL statement itself, instead of looping throu all the rows.
0
Accepted
Stefan
Telerik team
answered on 26 Dec 2012, 01:14 PM
Hello Philippe,

Indeed, with such amounts of data iterating the rows will be slow and the fastest approach will be to prepare your flag in your data base and then just use it in the formatting events.

I hope that you find this information useful.
 
Greetings,
Stefan
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Philippe
Top achievements
Rank 2
answered on 27 Dec 2012, 04:31 PM
It was so easy to program directly, I almost feel stupid not thinking about it beforehand.

Thanks for your help Stefan.

As a suggestion: a setting that automatically flag a row as distingushable from another by a column's value change, a bit like .IsOdd.
0
Stefan
Telerik team
answered on 28 Dec 2012, 12:39 PM
Thank you for your suggestion. We will consider it in future release.

I am glad that everything is now OK on your end.
 
All the best,
Stefan
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
Philippe
Top achievements
Rank 2
Answers by
Plamen
Telerik team
Philippe
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or