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

Add new row - does not appear in datatable

3 Answers 207 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lasse
Top achievements
Rank 1
Lasse asked on 19 Jan 2012, 05:09 PM
Hi,

Simple question that need a quick answer... :)
I have a grid that allows the user to add new rows. This grid is bounded to a datatable.
When i click on a new row and edit the first column and tab out of it, i expect the cell in the datatable to be the same as in the grid.
But this does not happen before i move to another row, or loose focus on the grid.

Since i'm doing a lot of validation on the data added to the datatable on the "CellValueChanged" etc, i need the data to be added to the datatable as well before changing rows. How can i achieve this? 

If i change a value in an already existing row in the grid, this functionality works as it should. So its only on the new rows added. 

Regards
Svein Thomas

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 24 Jan 2012, 02:39 PM
Hi Svein,

The reported behavior is desired. In bound mode when the new row is created, the related CurrencyManager will execute its AddNew method and it will create a new bound item. However, the commit operation is not processed before changing the current position or before calling the EndEdit method explicitly. The described workflow is valid for objects that implement the IEditableObject interface. DataTable and DataRow types for example implement it in order to support canceling. 

In this case the best option is calling the EndEdit method when handling CellValueChanged event:

void radGridView1_CellValueChanged(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    IEditableObject editbaleObject = e.Row.DataBoundItem as IEditableObject;
    if (editbaleObject != null)
    {
        editbaleObject.EndEdit();
    }
}

I hope this helps. Let me know if you need further assistance. 

Regards,
Julian Benkov
the Telerik team

SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

0
Lasse
Top achievements
Rank 1
answered on 24 Jan 2012, 02:45 PM
Hi and thanks for the answer,

The e.Row.DataBoundItem object is null in this case, so i'm not able to call EndEdit on the object.

Here are the code (with the EndEdit code) 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Globalization;
using Telerik.WinControls;
using System.Threading;
 
namespace TestRadGrid
{
    public partial class Form3 : Form
    {
        private DataTable workdayPatternTable = new DataTable();
        public Form3()
        {
            InitializeComponent();
            setupTable();
            addEventsHandlers();
            addTestData();
        }
 
        private void addEventsHandlers()
        {
            this.WorkdayPatternGrid.RowPaint += new GridViewRowPaintEventHandler(WorkdayPatternGrid_RowPaint);
            this.WorkdayPatternGrid.UserAddingRow += new GridViewRowCancelEventHandler(WorkdayPatternGrid_UserAddingRow);
            this.WorkdayPatternGrid.UserAddedRow += new GridViewRowEventHandler(WorkdayPatternGrid_UserAddedRow);
            this.WorkdayPatternGrid.DefaultValuesNeeded += new GridViewRowEventHandler(WorkdayPatternGrid_DefaultValuesNeeded);
            this.WorkdayPatternGrid.CellValueChanged += new GridViewCellEventHandler(WorkdayPatternGrid_CellValueChanged);
            this.WorkdayPatternGrid.CellFormatting += new CellFormattingEventHandler(WorkdayPatternGrid_CellFormatting);
            this.WorkdayPatternGrid.CellEditorInitialized += new GridViewCellEventHandler(WorkdayPatternGrid_CellEditorInitialized);
            this.WorkdayPatternGrid.CreateRow += new GridViewCreateRowEventHandler(WorkdayPatternGrid_CreateRow);
            this.WorkdayPatternGrid.ViewCellFormatting += new CellFormattingEventHandler(WorkdayPatternGrid_ViewCellFormatting);
            this.WorkdayPatternGrid.UserDeletedRow += new GridViewRowEventHandler(WorkdayPatternGrid_UserDeletedRow);
            this.WorkdayPatternGrid.UserDeletingRow += new GridViewRowCancelEventHandler(WorkdayPatternGrid_UserDeletingRow);
             
        }
        public int GetWeekNumber(DateTime dtPassed)
        {
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }
 
        private void AddRow(DataTable table, int rowID = 0, DateTime? start = null, DateTime? end = null, Double breakTime = 0, int dow = 0, int week = 0)
        {
 
            if (start.HasValue)
                dow = (int)start.Value.DayOfWeek;
 
            if (start.HasValue)
                week = GetWeekNumber(start.Value);
 
            DataRow[] rows = table.Select("WeekNumber = " + week.ToString());
            DataRow row = null;
            if (rows.Count() == 0)
                row = table.Rows.Add(rowID, week, dow);
            else
                row = rows[0];
 
            row["DayID" + dow] = rowID;
            if (start.HasValue)
                row["Start" + dow] = start.Value;
            if (end.HasValue)
                row["End" + dow] = end.Value;
            if (breakTime > 0)
                row["Break" + dow] = breakTime;
        }
 
        private void addTestData()
        {
            AddRow(workdayPatternTable, 1, new DateTime(1900, 01, 01, 07, 30, 00), new DateTime(1900, 01, 01, 15, 30, 00), .5);
            AddRow(workdayPatternTable, 2, new DateTime(1900, 01, 02, 14, 30, 00), new DateTime(1900, 01, 02, 22, 30, 00), .5);
            AddRow(workdayPatternTable, 3, new DateTime(1900, 01, 04, 22, 00, 00), new DateTime(1900, 01, 05, 07, 30, 00), .5);
            AddRow(workdayPatternTable, 4, new DateTime(1900, 01, 05, 22, 00, 00), new DateTime(1900, 01, 06, 07, 30, 00), .5);
            AddRow(workdayPatternTable, 5, new DateTime(1900, 01, 08, 07, 30, 00), new DateTime(1900, 01, 08, 15, 30, 00), .5);
            AddRow(workdayPatternTable, 6, new DateTime(1900, 01, 09, 07, 30, 00), new DateTime(1900, 01, 09, 15, 30, 00), .5);
            AddRow(workdayPatternTable, 7, new DateTime(1900, 01, 11, 14, 30, 00), new DateTime(1900, 01, 11, 22, 30, 00), .5);
            AddRow(workdayPatternTable, 8, new DateTime(1900, 01, 12, 07, 30, 00), new DateTime(1900, 01, 12, 15, 30, 00), .5);
            AddRow(workdayPatternTable, 9, new DateTime(1900, 01, 16, 07, 30, 00), new DateTime(1900, 01, 16, 22, 30, 00), .5);
        }
 
        private void setupTable()
        {
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
 
            workdayPatternTable.Columns.Add("ID", typeof(int));
            workdayPatternTable.Columns.Add("WeekNumber", typeof(int));
            workdayPatternTable.Columns.Add("WeekDay", typeof(int));
            workdayPatternTable.Columns.Add("DayID0", typeof(int));
            workdayPatternTable.Columns.Add("Start0", typeof(DateTime));
            workdayPatternTable.Columns.Add("End0", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break0", typeof(Double));
            workdayPatternTable.Columns.Add("DayID1", typeof(int));
            workdayPatternTable.Columns.Add("Start1", typeof(DateTime));
            workdayPatternTable.Columns.Add("End1", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break1", typeof(Double));
            workdayPatternTable.Columns.Add("DayID2", typeof(int));
            workdayPatternTable.Columns.Add("Start2", typeof(DateTime));
            workdayPatternTable.Columns.Add("End2", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break2", typeof(Double));
            workdayPatternTable.Columns.Add("DayID3", typeof(int));
            workdayPatternTable.Columns.Add("Start3", typeof(DateTime));
            workdayPatternTable.Columns.Add("End3", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break3", typeof(Double));
            workdayPatternTable.Columns.Add("DayID4", typeof(int));
            workdayPatternTable.Columns.Add("Start4", typeof(DateTime));
            workdayPatternTable.Columns.Add("End4", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break4", typeof(Double));
            workdayPatternTable.Columns.Add("DayID5", typeof(int));
            workdayPatternTable.Columns.Add("Start5", typeof(DateTime));
            workdayPatternTable.Columns.Add("End5", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break5", typeof(Double));
            workdayPatternTable.Columns.Add("DayID6", typeof(int));
            workdayPatternTable.Columns.Add("Start6", typeof(DateTime));
            workdayPatternTable.Columns.Add("End6", typeof(DateTime));
            workdayPatternTable.Columns.Add("Break6", typeof(Double));
             
             
            this.WorkdayPatternGrid.EnableCustomDrawing = true;
            this.WorkdayPatternGrid.AutoGenerateColumns = false;
            this.WorkdayPatternGrid.EnableGrouping = false;
            this.WorkdayPatternGrid.AddNewRowPosition = SystemRowPosition.Bottom;
            this.WorkdayPatternGrid.DataSource = workdayPatternTable;
            this.WorkdayPatternGrid.ShowRowHeaderColumn = false;
            this.WorkdayPatternGrid.AllowDeleteRow = true;
            this.WorkdayPatternGrid.AllowAddNewRow = true;
 
            workdayPatternTable.ColumnChanged += new DataColumnChangeEventHandler(workdayPatternTable_ColumnChanged); ;
            GridViewTextBoxColumn idcol = new GridViewTextBoxColumn("ID");
            idcol.IsVisible = false;
 
            GridViewTextBoxColumn weekcol = new GridViewTextBoxColumn("WeekNumber");
            weekcol.HeaderText = Properties.Resources.Weekno;
            weekcol.TextAlignment = ContentAlignment.MiddleCenter;
            weekcol.ReadOnly = true;
            weekcol.Width = 38;
 
 
            //GridViewImageColumn errcol = new GridViewImageColumn("ErrCol");
            //errcol.HeaderText = "";
            //errcol.TextAlignment = ContentAlignment.MiddleCenter;
            //errcol.ReadOnly = true;
            //errcol.Width = 20;
 
 
 
            this.WorkdayPatternGrid.Columns.Add(idcol);
            this.WorkdayPatternGrid.Columns.Add(weekcol);
            //this.WorkdayPatternGrid.Columns.Add(errcol);
 
            for (int i = 0; i < 7; i++)
            {
                GridViewTextBoxColumn dayidcol = new GridViewTextBoxColumn("DayID" + i, "DayID" + i);
                GridViewTextBoxColumn startcol = new GridViewTextBoxColumn("Start" + i, "Start" + i);
                GridViewTextBoxColumn endcol = new GridViewTextBoxColumn("End" + i, "End" + i);
                GridViewTextBoxColumn breakcol = new GridViewTextBoxColumn("Break" + i, "Break" + i);
 
                dayidcol.IsVisible = false;
 
                startcol.FormatString = "{0:HH:mm}";
                startcol.TextAlignment = ContentAlignment.MiddleCenter;
                startcol.HeaderText = Properties.Resources.Start;
                startcol.DataTypeConverter = new DateConverter();
                startcol.Width = 45;
                startcol.MaxLength = 5;
 
                endcol.FormatString = "{0:HH:mm}";
                endcol.TextAlignment = ContentAlignment.MiddleCenter;
                endcol.HeaderText = Properties.Resources.End;
                endcol.DataTypeConverter = new DateConverter();
                endcol.Width = 45;
                endcol.MaxLength = 5;
 
                breakcol.TextAlignment = ContentAlignment.MiddleCenter;
                breakcol.HeaderText = Properties.Resources.Break;
                breakcol.Width = 40;
                //breakcol.AutoSizeMode = BestFitColumnMode.HeaderCells;
 
                this.WorkdayPatternGrid.Columns.Add(dayidcol);
                this.WorkdayPatternGrid.Columns.Add(startcol);
                this.WorkdayPatternGrid.Columns.Add(endcol);
                this.WorkdayPatternGrid.Columns.Add(breakcol);
            }
 
            ColumnGroupsViewDefinition def = new ColumnGroupsViewDefinition();
 
            def.ColumnGroups.Add(new GridViewColumnGroup("Event"));
            def.ColumnGroups[0].ShowHeader = false;
            def.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            def.ColumnGroups[0].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns[0]);
            def.ColumnGroups[0].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns[1]);
 
            for (int i = 0; i < 7; i++)
            {
 
                def.ColumnGroups.Add(new GridViewColumnGroup(ci.DateTimeFormat.DayNames.ToList()[i]));
 
                def.ColumnGroups[1 + i].Rows.Add(new GridViewColumnGroupRow());
                def.ColumnGroups[1 + i].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns["DayID" + i]);
                def.ColumnGroups[1 + i].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns["Start" + i]);
                def.ColumnGroups[1 + i].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns["End" + i]);
                def.ColumnGroups[1 + i].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns["Break" + i]);
 
            }
 
            var fdow = (int)ci.DateTimeFormat.FirstDayOfWeek;
            for (int i = 0; i < fdow; i++)
            {
                def.ColumnGroups.Move(1 + i, def.ColumnGroups.Count() - 1);
            }
 
            //def.ColumnGroups.Add(new GridViewColumnGroup("Errors"));
            //def.ColumnGroups[8].ShowHeader = false;
            //def.ColumnGroups[8].Rows.Add(new GridViewColumnGroupRow());
            //def.ColumnGroups[8].Rows[0].Columns.Add(this.WorkdayPatternGrid.Columns[2]);
 
 
            this.WorkdayPatternGrid.ViewDefinition = def;
        }
 
        void workdayPatternTable_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            //throw new NotImplementedException();
        }
 
        void WorkdayPatternGrid_CreateRow(object sender, GridViewCreateRowEventArgs e)
        {
            Console.WriteLine("GridRow Created");
        }
 
        void WorkdayPatternGrid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            RadTextBoxEditor editor = e.ActiveEditor as RadTextBoxEditor;
            GridViewTextBoxColumn dataColumn = e.Column as GridViewTextBoxColumn;
 
            RadTextBoxEditorElement editorElement = editor.EditorElement as RadTextBoxEditorElement;
            if (editor != null && dataColumn != null && (dataColumn.Name.StartsWith("Start") || dataColumn.Name.StartsWith("End")))
            {
                editorElement.TextBoxItem.TextChanging -= new TextChangingEventHandler(TextBoxItem_TextChanging);
                editorElement.TextBoxItem.TextChanging += new TextChangingEventHandler(TextBoxItem_TextChanging);
                //editorElement.TextBoxItem.Validated -= new EventHandler(TextBoxItem_Validated);
                //editorElement.TextBoxItem.Validated += new EventHandler(TextBoxItem_Validated);
                editorElement.TextBoxItem.KeyPress -= new KeyPressEventHandler(TextBoxItem_KeyPress);
                editorElement.TextBoxItem.KeyPress += new KeyPressEventHandler(TextBoxItem_KeyPress);
 
            }
            else
            {
                editorElement.TextBoxItem.TextChanging -= TextBoxItem_TextChanging;
                editorElement.TextBoxItem.KeyPress -= TextBoxItem_KeyPress;
            }
 
        }
 
        //void TextBoxItem_Validated(object sender, EventArgs e)
        //{
        //    ColumnGroupsViewDefinition def = this.WorkdayPatternGrid.ViewDefinition as ColumnGroupsViewDefinition;
        //    var columnGroup = def.ColumnGroups.SingleOrDefault(cg => cg.Rows[0].Columns.Any(c => c.Name == WorkdayPatternGrid.CurrentColumn.Name));
        //    if (ValidateColumnGroup(columnGroup, WorkdayPatternGrid.CurrentRow))
        //        ValidateWithTariff(CurrentTariff);
        //}
 
        void TextBoxItem_KeyPress2(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                                && !char.IsDigit(e.KeyChar)
                                && e.KeyChar != Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0])
            {
                e.Handled = true;
            }
 
            // only allow one decimal point
            if (e.KeyChar == Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]
                && (sender as RadTextBoxItem).Text.IndexOf(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]) > -1)
            {
                e.Handled = true;
            }
        }
 
        void TextBoxItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != ':')
            {
                e.Handled = true;
            }
 
            // only allow one time seperator at place 3 00:00
            if (e.KeyChar == ':'
                && (sender as RadTextBoxItem).Text.IndexOf(':') != 3)
            {
                e.Handled = true;
            }
 
        }
 
        void TextBoxItem_TextChanging(object sender, TextChangingEventArgs e)
        {
            CorrectTime(sender, e);
        }
 
        private void CorrectTime(object sender, Telerik.WinControls.TextChangingEventArgs e)
        {
            if (e.NewValue.Length == 3 && e.NewValue.IndexOf(":") == -1)
            {
                ((RadTextBoxItem)sender).Text = e.NewValue.Insert(2, ":");
                ((RadTextBoxItem)sender).SelectionStart = 4;
            }
        }
 
        void WorkdayPatternGrid_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridDataCellElement cell = e.CellElement as GridDataCellElement;
            if (cell != null)
            {
 
 
 
                ColumnGroupsViewDefinition def = this.WorkdayPatternGrid.ViewDefinition as ColumnGroupsViewDefinition;
                var columnGroup = def.ColumnGroups.SingleOrDefault(cg => cg.Rows[0].Columns.Any(c => c.Name == e.Column.Name));
                e.CellElement.DrawFill = true;
 
                e.CellElement.NumberOfColors = 1;
                e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
 
                if (def.ColumnGroups.IndexOf(columnGroup) == 0)
                {
                    e.CellElement.BackColor = Color.LightGoldenrodYellow;
                    e.CellElement.BorderInnerColor = Color.LightGoldenrodYellow;
                }
                else
                {
                    if (def.ColumnGroups.IndexOf(columnGroup) % 2 == 0)
                    {
                        e.CellElement.BackColor = Color.PowderBlue;
                        e.CellElement.BorderInnerColor = Color.PowderBlue;
                    }
                    else
                    {
                        e.CellElement.BackColor = Color.PapayaWhip;
                        e.CellElement.BorderInnerColor = Color.PapayaWhip;
                    }
                }
 
                if (cell.ContainsErrors)
                {
                    cell.DrawBorder = true;
                    cell.BorderBoxStyle = BorderBoxStyle.SingleBorder;
                    //cell.NumberOfColors = 1;
                    cell.BorderWidth = 2;
                    cell.BorderColor = Color.Red;
                    cell.BorderColor2 = Color.Red;
                    cell.BorderColor3 = Color.Red;
                    cell.BorderColor4 = Color.Red;
                }
                else
                {
                    cell.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderWidthProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local);
                    //                    cell.ResetValue(LightVisualElement.BorderInnerColorProperty, ValueResetFlags.Local);
                }
            }
        }
 
        void WorkdayPatternGrid_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement is GridHeaderCellElement)
            {
                ColumnGroupsViewDefinition def = this.WorkdayPatternGrid.ViewDefinition as ColumnGroupsViewDefinition;
 
                GridViewColumnGroup columnGroup = null;
                if (e.CellElement is GridColumnGroupCellElement)
                    columnGroup = def.ColumnGroups.SingleOrDefault(cg => cg.Text == e.CellElement.ColumnInfo.HeaderText);
                else
                    columnGroup = def.ColumnGroups.SingleOrDefault(cg => cg.Rows[0].Columns.Any(c => c.Name == e.CellElement.ColumnInfo.Name));
 
 
                var col = def.ColumnGroups.IndexOf(columnGroup) % 2;
 
 
                e.CellElement.DrawFill = true;
                //e.CellElement.DrawBorder = false;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
 
                if (def.ColumnGroups.IndexOf(columnGroup) == 0)
                {
                    e.CellElement.BackColor = Color.LightGoldenrodYellow;
                    return;
                }
 
 
                if (col == 0)
                {
                    e.CellElement.BackColor = Color.PowderBlue;
                }
                else
                {
                    e.CellElement.BackColor = Color.PapayaWhip;
                }
 
            }
        }
 
        void WorkdayPatternGrid_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            IEditableObject editbaleObject = e.Row.DataBoundItem as IEditableObject;
            if (editbaleObject != null)
            {
                editbaleObject.EndEdit();
            }
            Console.WriteLine("CellValue Changed in grid");
            ColumnGroupsViewDefinition def = this.WorkdayPatternGrid.ViewDefinition as ColumnGroupsViewDefinition;
            var columnGroup = def.ColumnGroups.SingleOrDefault(cg => cg.Rows[0].Columns.Any(c => c.Name == e.Column.Name));
            //if (ValidateColumnGroup(columnGroup, e.Row))
            //    ValidateWithTariff(CurrentTariff);
             
        }
 
        void WorkdayPatternGrid_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
        {
            Console.WriteLine("Default value needed");
            //AddRow(workdayPatternTable, week: workdayPatternTable.Rows.Count + 1);
            e.Row.Cells[1].Value = workdayPatternTable.Rows.Count + 1;
        }
 
        void WorkdayPatternGrid_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
        {
            Console.WriteLine("User adding row");
            if (e.Rows[0].ErrorText.Length > 0)
            {
                e.Cancel = true;
                return;
            }
 
            //if (!ValidateTable())
            //{
            //    e.Cancel = true;
            //    return;
            //}
 
            //if (!ValidateWithTariff(CurrentTariff))
            //{
            //    e.Cancel = true;
            //    return;
            //}
        }
 
        void WorkdayPatternGrid_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            Console.WriteLine("User added row");
        }
 
        private void WorkdayPatternGrid_CreateRowInfo(object sender, GridViewCreateRowInfoEventArgs e)
        {
            Console.WriteLine("Created Row info");
            e.RowInfo.Height = 25;
 
        }
 
        private void WorkdayPatternGrid_RowPaint(object sender, GridViewRowPaintEventArgs e)
        {
            if (e.Row is GridDataRowElement)
            {
                GridDataRowElement rowElement = (GridDataRowElement)e.Row;
 
            }
        }
 
        void WorkdayPatternGrid_UserDeletingRow(object sender, GridViewRowCancelEventArgs e)
        {
            workdayPatternTable.Rows[e.Rows[0].Index].Delete();
 
        }
 
        void WorkdayPatternGrid_UserDeletedRow(object sender, GridViewRowEventArgs e)
        {
        }
    }
 
    public class DateConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(DateTime);
        }
 
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is string)
            {
                string txt = value as string;
 
                switch (txt.Length)
                {
                    case 0:
                        txt = "00:00";
                        break;
                    case 1:
                        txt = "00:0" + txt;
                        break;
                    case 2:
                        if (txt.StartsWith("0"))
                            txt = txt + ":00";
                        else
                            txt = "00:" + txt;
                        break;
                    case 3:
                        txt = txt + "00";
                        break;
                    case 4:
                        if (txt.StartsWith("0"))
                            txt = txt + "0";
                        else
                            txt = "0" + txt.Substring(0, 1) + ":" + txt.Substring(1, 1) + txt.Substring(3, 1);
                        break;
                }
 
                int hours = int.Parse(txt.Substring(0, 2));
                int minutes = int.Parse(txt.Substring(3, 2));
 
                if (hours > 23) hours = 23;
                if (minutes > 59) minutes = 59;
 
                DateTime date = DateTime.Now;
                return new DateTime(1900, 1, 1, hours, minutes, 0);
            }
 
            if (value is DateTime && destinationType == typeof(DateTime))
            {
                return value;
            }
 
            return base.ConvertTo(context, culture, value, destinationType);
        }
 
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(DateTime);
        }
 
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            DateTime dateValue = (DateTime)value;
            return dateValue.ToString("HH:mm");
        }
    }
}

namespace TestRadGrid
{
    partial class Form3
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.WorkdayPatternGrid = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.WorkdayPatternGrid)).BeginInit();
            this.SuspendLayout();
            //
            // WorkdayPatternGrid
            //
            this.WorkdayPatternGrid.BackColor = System.Drawing.SystemColors.Control;
            this.WorkdayPatternGrid.Cursor = System.Windows.Forms.Cursors.Default;
            this.WorkdayPatternGrid.Font = new System.Drawing.Font("Segoe UI", 8.25F);
            this.WorkdayPatternGrid.ForeColor = System.Drawing.SystemColors.ControlText;
            this.WorkdayPatternGrid.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.WorkdayPatternGrid.Location = new System.Drawing.Point(11, 30);
            this.WorkdayPatternGrid.Name = "WorkdayPatternGrid";
            this.WorkdayPatternGrid.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.WorkdayPatternGrid.ShowGroupPanel = false;
            this.WorkdayPatternGrid.Size = new System.Drawing.Size(787, 244);
            this.WorkdayPatternGrid.TabIndex = 0;
            this.WorkdayPatternGrid.Text = "radGridView1";
            //
            // Form3
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(843, 316);
            this.Controls.Add(this.WorkdayPatternGrid);
            this.Name = "Form3";
            this.Text = "Form3";
            ((System.ComponentModel.ISupportInitialize)(this.WorkdayPatternGrid)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView WorkdayPatternGrid;
    }
}

Regards
Svein Thomas
0
Accepted
Julian Benkov
Telerik team
answered on 27 Jan 2012, 10:34 AM
Hello Svein,

To support this editing functionality in your application, you must also enable inserting a bound row on user input in RadGridView new row. The property for this behavior is AddNewBoundRowBeforeEdit. Here is the modified code snippet:

private void addEventsHandlers()
{
    this.WorkdayPatternGrid.RowPaint += new GridViewRowPaintEventHandler(WorkdayPatternGrid_RowPaint);
    this.WorkdayPatternGrid.UserAddingRow += new GridViewRowCancelEventHandler(WorkdayPatternGrid_UserAddingRow);
    this.WorkdayPatternGrid.UserAddedRow += new GridViewRowEventHandler(WorkdayPatternGrid_UserAddedRow);
    this.WorkdayPatternGrid.DefaultValuesNeeded += new GridViewRowEventHandler(WorkdayPatternGrid_DefaultValuesNeeded);
    this.WorkdayPatternGrid.CellValueChanged += new GridViewCellEventHandler(WorkdayPatternGrid_CellValueChanged);
    this.WorkdayPatternGrid.CellFormatting += new CellFormattingEventHandler(WorkdayPatternGrid_CellFormatting);
    this.WorkdayPatternGrid.CellEditorInitialized += new GridViewCellEventHandler(WorkdayPatternGrid_CellEditorInitialized);
    this.WorkdayPatternGrid.CreateRow += new GridViewCreateRowEventHandler(WorkdayPatternGrid_CreateRow);
    this.WorkdayPatternGrid.ViewCellFormatting += new CellFormattingEventHandler(WorkdayPatternGrid_ViewCellFormatting);
    this.WorkdayPatternGrid.UserDeletedRow += new GridViewRowEventHandler(WorkdayPatternGrid_UserDeletedRow);
    this.WorkdayPatternGrid.UserDeletingRow += new GridViewRowCancelEventHandler(WorkdayPatternGrid_UserDeletingRow);
 
    this.WorkdayPatternGrid.MasterTemplate.AddNewBoundRowBeforeEdit = true;
}

Do not hesitate to contact us if you have further questions or issues.

All the best,
Julian Benkov
the Telerik team

SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

Tags
GridView
Asked by
Lasse
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Lasse
Top achievements
Rank 1
Share this question
or