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

Customize New Row

3 Answers 189 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dreyfus
Top achievements
Rank 1
Veteran
Dreyfus asked on 09 Jul 2020, 09:14 AM

Quote from https://docs.telerik.com/devtools/winforms/controls/gridview/rows/formatting-rows:

"To customize the non-data rows (header row, new row, filtering row, etc) of RadGridView, you need to handle the ViewRowFormatting event."

 

In that regard, how can I detect newly added row/s from ViewRowFormatting event?

Please know that I couldn't use the CreateRow event since it uses UI virtualization and it doesn't trigger for newly added rows that are not "visible" on the screen.

3 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 09 Jul 2020, 02:25 PM

Hello, Dreyfus,

Yes, ViewRowFormatting event fires for all non-data rows including a new row. According to the provided information, it seems that you would like to detect which are the newly added rows and customize them in a formatting event.

If this is your requirement, I can suggest you to store information about which row is newly added by using the Tag property. The Tag property is of type object and can store any useful information about the row. Note that RadGridView offers the UserAddedRow event where you can store the information about the row. Then, in the ViewRowFormatting even you have access to the GridViwRowInfo through the event arguments e.RowElement.RowInfo and you can color the appropriate rows that are newly added. Please refer to the following code snippet that demonstrates this approach:

 public Form1()
 {
     InitializeComponent(); 


     this.radGridView1.ViewRowFormatting += this.RadGridView1_ViewRowFormatting;
     this.radGridView1.UserAddedRow += this.RadGridView1_UserAddedRow;
     DataTable dt = new DataTable();

     dt.Columns.Add("Product ID", typeof(int));
     dt.Columns.Add("Product Name", typeof(string));
     dt.Columns.Add("Manufacturer", typeof(string));
     dt.Columns.Add("Sales Representant", typeof(string));

     dt.Rows.Add(15, "Wooden Table", "Molzano Italy", "Simonette Saylor");
     dt.Rows.Add(22, "Glass Table", "Ovalia Germany", "Richter Strauss");
     dt.Rows.Add(31, "Small Glass Table", "L'Ouruje France", "Fannie Duncan");
     dt.Rows.Add(14, "White Leather Chair", "Spidea England", "Paolo Rossini");
     dt.Rows.Add(25, "Black Leather Couch", "Procren Hungary", "Jaroslav Lokash");
     dt.Rows.Add(16, "Red Leather Couch", "Pront Italy", "Hellena Shnaider ");
     dt.Rows.Add(37, "Modern Design Chair", "Atech USA", "Silvester Williams");
     this.radGridView1.DataSource = dt;
 }

private void RadGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
    GridViewRowInfo row = e.Row;
    row.Tag = "new row";
}

private void RadGridView1_ViewRowFormatting(object sender, RowFormattingEventArgs e)
{
   if ( (string)e.RowElement.RowInfo.Tag == "new row")
    {
        e.RowElement.DrawFill = true;
        e.RowElement.BackColor = Color.LightSkyBlue;
        e.RowElement.NumberOfColors = 1;
        e.RowElement.ForeColor = Color.White;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
    }
}

I hope this helps. Should you have further questions I will be glad to help.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dreyfus
Top achievements
Rank 1
Veteran
answered on 10 Jul 2020, 12:41 AM

Thanks for the reply Nadya.

But in my case, I add rows by using the RowCount property (ie: GridView.RowCount = 5), and I'm afraid UseAddedRow event is not triggered by this property.

Any other ways?

0
Nadya | Tech Support Engineer
Telerik team
answered on 14 Jul 2020, 12:12 PM

Hello,

If you add new rows by using the RowCount property I suppose that you use RadGridView in Virtual mode, and you would like to color the newly added rows. If this is your requirement, I can suggest storing the indexes of the rows in separate variables before and after the new rows are added, and later use them in the CellFormatting event to style them accordingly. Please refer to the following example:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    DataTable dt;
    public RadForm1()
    {
        InitializeComponent();
        RadControlSpyForm spyForm = new RadControlSpyForm();
        spyForm.Show();

        this.radGridView1.AllowAddNewRow = true;
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.CellFormatting += this.RadGridView1_CellFormatting;

         dt = new DataTable();

        dt.Columns.Add("Product ID", typeof(int));
        dt.Columns.Add("Product Name", typeof(string));
        dt.Columns.Add("Manufacturer", typeof(string));
        dt.Columns.Add("Sales Representant", typeof(string));

        dt.Rows.Add(15, "Wooden Table", "Molzano Italy", "Simonette Saylor");
        dt.Rows.Add(22, "Glass Table", "Ovalia Germany", "Richter Strauss");
        dt.Rows.Add(31, "Small Glass Table", "L'Ouruje France", "Fannie Duncan");
        dt.Rows.Add(14, "White Leather Chair", "Spidea England", "Paolo Rossini");
        dt.Rows.Add(25, "Black Leather Couch", "Procren Hungary", "Jaroslav Lokash");
        
        this.radGridView1.CellValueNeeded += new GridViewCellValueEventHandler(radGridView1_CellValueNeeded);
        radGridView1.VirtualMode = true;
        radGridView1.ColumnCount =dt.Columns.Count;
        this.radGridView1.RowCount = dt.Rows.Count;
        
    }

    private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.RowInfo.Index >= rangeStart && e.CellElement.RowInfo.Index <= rangeEnd && newrows)
        {
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.LightSkyBlue;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.ForeColor = Color.White;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
        }
    }

    private void radGridView1_CellValueNeeded(object sender, GridViewCellValueEventArgs e)
    {
      
        e.Value = dt.Rows[e.RowIndex][e.ColumnIndex];
        
    }

    int rangeStart;
    int rangeEnd;
    bool newrows = false;
    private void radButton1_Click(object sender, EventArgs e)
    {
        rangeStart = dt.Rows.Count ;
        

        dt.Rows.Add(16, "Red Leather Couch", "Pront Italy", "Hellena Shnaider ");
        dt.Rows.Add(37, "Modern Design Chair", "Atech USA", "Silvester Williams");
        this.radGridView1.RowCount = dt.Rows.Count;
        newrows = true;

        rangeEnd = dt.Rows.Count - 1;
        this.radGridView1.CurrentRow = this.radGridView1.Rows[this.radGridView1.Rows.Count-1];
    }
}

I hope this helps. If you are experiencing further difficulties do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Tags
GridView
Asked by
Dreyfus
Top achievements
Rank 1
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Dreyfus
Top achievements
Rank 1
Veteran
Share this question
or