using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace ViewCellFormatting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dgvMap.TableElement.RowHeaderColumnWidth = 80;

            int numberOfRow = 55;
            int numberOfColumn = 77;

            for (int j = 1; j <= numberOfColumn; j++)
                dgvMap.Columns.Add(string.Format("C{0}", j));


            for (int k = 1; k <= numberOfRow; k++)
            {
                dgvMap.Rows.Add(new string[] { "" });
            }

            for (int k = 1; k <= numberOfRow; k++)
            {
                for (int j = 1; j <= numberOfColumn; j++)
                {
                    dgvMap.Rows[k - 1].Cells[j - 1].Value = 0;
                }
            }
        }

        private void dgvMap_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
        {
            if (!(e.Row is GridViewDataRowInfo))
            {
                return;
            }

            if (e.CellElement is GridRowHeaderCellElement)
            {

                e.CellElement.Text = string.Format("R{0}", dgvMap.CurrentView.ViewInfo.Rows.IndexOf(e.Row) + 1);
                var element = new RadButtonElement();
                element.Margin = new Padding(3, 3, 3, 3);
                element.Padding = new Padding(2, 0, 2, -2);
                element.ImageAlignment = ContentAlignment.MiddleCenter;
                element.Alignment = ContentAlignment.MiddleCenter;
                element.ForeColor = Color.Black;
                element.Text = string.Format("R{0}", dgvMap.CurrentView.ViewInfo.Rows.IndexOf(e.Row) + 1);
                element.AutoSizeMode = RadAutoSizeMode.Auto;
                element.MinSize = new Size(80, 50);
                e.CellElement.Children.Add(element);
                e.CellElement.ForeColor = Color.White;
                dgvMap.Rows[e.RowIndex].Tag = string.Format("R{0}", dgvMap.CurrentView.ViewInfo.Rows.IndexOf(e.Row) + 1);

            }

            if (e.CellElement is GridCellElement && e.CellElement.Text.Trim().Length != 0)
            {

                    Color c = Color.Black;
                    e.CellElement.ForeColor = Color.Black;
                    e.CellElement.BackColor = c;
                    e.CellElement.DrawFill = true;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            }
        }
    }
}
