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

GridViewRow paint

6 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Atefeh
Top achievements
Rank 1
Atefeh asked on 17 May 2012, 08:39 AM
Hi
I have a question about Row paint in rad grid view.
I have Rowpaint event in my grid that paint some cell in row and i use some button that specify the color of row. when I change the color of button row color changed but  when i move the mouse in grid. I want to change color automatically.
 How can I do this?

6 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 21 May 2012, 04:19 PM
Hello Atefeh,

Thank you for writing.

In order to apply the new painting the grid should be re-painted. This can be achieved by calling its Invalidate method. Here is a sample application:
public partial class Form1 : Form
{
    Pen pen;
 
    public Form1()
    {
        InitializeComponent();
 
        Random r = new Random();
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Bool", typeof(bool));
        table.Columns.Add("DateColumn", typeof(DateTime));
 
        for (int i = 0; i < 10; i++)
        {
            table.Rows.Add(i, "Row " + i, r.Next(10) > 5 ? true : false, DateTime.Now.AddHours(i));
        }
 
        this.radGridView1.DataSource = table;
        radGridView1.EnableCustomDrawing = true;
 
        radGridView1.RowPaint += new Telerik.WinControls.UI.GridViewRowPaintEventHandler(radGridView1_RowPaint);
        pen = new Pen(Color.Red, 3);
    }
 
    void radGridView1_RowPaint(object sender, Telerik.WinControls.UI.GridViewRowPaintEventArgs e)
    {
        GridDataRowElement dataRow = e.Row as GridDataRowElement;
 
        if (dataRow != null)
        {
            double value = Convert.ToDouble(dataRow.RowInfo.Cells["ID"].Value);
 
            Size rowSize = dataRow.Size;
            rowSize.Height -= 6;
            rowSize.Width -= 5;
            e.Graphics.DrawRectangle(pen, new Rectangle(new Point(2, 2), rowSize));
        }
    }
 
    private void radButton1_Click(object sender, EventArgs e)
    {
        if (radColorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pen = new Pen(radColorDialog1.SelectedColor, 3);
            radGridView1.Invalidate();
        }
    }
}

I hope this helps.
 
All the best,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Roya
Top achievements
Rank 1
answered on 26 May 2012, 06:21 AM
Hello Stefan
thanks for your answer, but when I use this
radGridView1.Invalidate()
in grid-Rowpaint function I can't use  scroll in my grid.
It's work just when I use this in another function not directly in  grid_RowPaint function.
            
0
Svett
Telerik team
answered on 30 May 2012, 04:15 PM
Hi Roya,

You should not call the Invalidate method of RadGridView inside the RowPaint event handler. The Invalidate method cause repainting of the whole control, which apparently raises the RowPaint event. I suppose that this causes the scroll bar issue. If the proposed solution does not resolve the issue, could you share a code snippet that demonstrates your approach?

Kind regards,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Atefeh
Top achievements
Rank 1
answered on 31 May 2012, 06:37 AM
HI Svett

in my project i want to change the color of border of row paint. To do this i selected with mouse and push the button 'btnSelected' and set the select column in data table to 1and then I want to change the color of border but It's happen after I click on then grid.
I should be say that if i have a button that only change the color of row that i use the Invalidate method it's work right but in that case, that i want happen after change data table, it doesn't work.
I attach some of my project code.
Thanks for your help.

   private void btnSelected_Click(object sender, EventArgs e)
        {
            string STime = "00:00";
            string ETime;
            string weekDay;
            int CountSelect = 0;
            int StartSelect = 0;
            Boolean flg = false;
            DataTable dt = new DataTable();

            for (int i = 0; i < grd.Rows.Count; i++)
            {
                flg = false;
                CountSelect = 0;
                for (int j = 0; j < grd.Columns.Count; j++)
                {

                    if (grd.Rows[i].Cells[j].IsSelected == true)
                    {
                        if (flg == false)
                        {
                            StartSelect = j;
                            STime = grd.Columns[j].Name;
                            weekDay = dtDate.Rows[i]["WeekDayNum"].ToString();
                            flg = true;
                        }
                        CountSelect++;
                    }


                }
                if (CountSelect != 0)
                {

                    ETime = grd.Columns[CountSelect + StartSelect].Name;
                    dt = dtTaghvim.Copy();
                    dt.DefaultView.RowFilter = "Date ='" + grd.Rows[i].Cells["date"].Value.ToString() + "' and Stime >='" + STime + "' and Etime<='" + ETime + "'";


                    for (int f = 0; f < dt.DefaultView.ToTable().Rows.Count; f++)
                    {
                        for (int d = 0; d < dtTaghvim.Rows.Count; d++)
                        {
                            if (dtTaghvim.Rows[d]["Date"].ToString() == dt.DefaultView.ToTable().Rows[f]["Date"].ToString()
                                && dtTaghvim.Rows[d]["STime"].ToString() == dt.DefaultView.ToTable().Rows[f]["STime"].ToString() &&
                                dtTaghvim.Rows[d]["ETime"].ToString() == dt.DefaultView.ToTable().Rows[f]["ETime"].ToString())
                            {
                                dtTaghvim.Rows[d]["Selected"] = 1;
                                grd.Invalidate();
                            }

                        }
                    }

                }

            }
        }

    private void grd_RowPaint(object sender, GridViewRowPaintEventArgs e)
        {

            GridDataRowElement dataRow = e.Row as GridDataRowElement;

            if (dataRow != null)
            {
                if (dataRow.RowInfo.Cells[0].ColumnInfo.Name == "Row")
                {

                    for (int j = 0; j < dtTaghvim.Rows.Count; j++)
                    {

                        if (dataRow.RowInfo.Cells["Date"].Value.ToString() == dtTaghvim.Rows[j]["Date"].ToString())
                        {


                            int startIndex = dataRow.RowInfo.Cells[findLeft(dtTaghvim.Rows[j]["STime"].ToString())].ColumnInfo.Index;

                            int endIndex = dataRow.RowInfo.Cells[findRight(dtTaghvim.Rows[j]["ETime"].ToString())].ColumnInfo.Index - 1;
                            GridDataCellElement startCell = GetCell(e.Row, startIndex);
                            GridDataCellElement endCell = GetCell(e.Row, endIndex);

                            if (startCell != null || endCell != null)
                            {
                                int left = startCell == null ? dataRow.ScrollableColumns.BoundingRectangle.Left - 10 : startCell.ControlBoundingRectangle.Left;
                                int right = endCell == null ? dataRow.ScrollableColumns.BoundingRectangle.Right + 10 : endCell.ControlBoundingRectangle.Right;
                                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                                Region clipRegion = e.Graphics.Clip;
                                e.Graphics.SetClip(dataRow.ScrollableColumns.BoundingRectangle);
                                Color colColor = new Color();
                                string Descr = "";
                                Color BorderColor = new Color();
                                if (dtTaghvim.Rows[j]["GenRef"].ToString() == "28")
                                {
                                    colColor = btnSansMen.ButtonElement.ButtonFillElement.BackColor;

                                }
                                else if (dtTaghvim.Rows[j]["GenRef"].ToString() == "29")
                                {

                                    colColor = btnSansWomen.ButtonElement.ButtonFillElement.BackColor;

                                }
                                else if (dtTaghvim.Rows[j]["GenRef"].ToString() == "30")
                                {

                                    colColor = btnNotdefined.ButtonElement.ButtonFillElement.BackColor;

                                }
                                if (dtTaghvim.Rows[j]["selected"].ToString() == "0")
                                {
                                    BorderColor = colColor;
                                }
                                else if (dtTaghvim.Rows[j]["selected"].ToString() == "1")
                                {

                                    BorderColor = Color.DarkBlue;
                                }
                                foreach (string ch in FieldName)
                                {
                                    if (dtTaghvim.Rows[j][ch].ToString() == "True")
                                    {
                                        dtTypeDesc.DefaultView.RowFilter =" descr= '" + ch +"'";
                                        Descr = Descr + "," + dtTypeDesc.DefaultView.ToTable().Rows[0]["Title"].ToString();
                                    }
                                }


                                using (RoundRectShape shape = new RoundRectShape(3))

                                using (GraphicsPath path = shape.CreatePath(new RectangleF(left + modL, 5, (right + modR) - (left + modL), e.Row.Size.Height - 10)))
                                using (Brush brush = new SolidBrush(colColor))
                                using (Brush brush1 = new SolidBrush(Color.Black))



                                using (Pen pen = new Pen(BorderColor, 2))
                                {

                                    e.Graphics.FillPath(brush, path);
                                    e.Graphics.DrawPath(pen, path);
                                    e.Graphics.DrawString(Descr ,Font, brush1, left + modL, 2);



                                }
                                e.Graphics.Clip = clipRegion;
                                //e.Row.ResumeLayout(true);
                                //grd.Invalidate();
                            }

                        }
                    }
                }
            }
        }    

0
Accepted
Svett
Telerik team
answered on 01 Jun 2012, 04:57 PM
Hello Atefeh,

You should handle the ViewChanged event of MasterTemplate where you can perform repainting of the control:

this.radGridView1.MasterTemplate.ViewChanged += new DataViewChangedEventHandler(MasterTemplate_ViewChanged);

private void MasterTemplate_ViewChanged(object sender, DataViewChangedEventArgs args)
{
    if (args.Action == ViewChangedAction.ItemChanged)
    {
        this.radGridView1.Invalidate();
    }
}

I hope this helps.

Kind regards,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Atefeh
Top achievements
Rank 1
answered on 05 Jun 2012, 04:14 AM
Hi Svett
thanks a lot it's work.
Tags
GridView
Asked by
Atefeh
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Roya
Top achievements
Rank 1
Svett
Telerik team
Atefeh
Top achievements
Rank 1
Share this question
or