I have a gridview bound to an BindingList<> and it allows the user to add a new row. Whenever the user clicks on the add new row area, the grid immediately inserts 2 new rows one of them overlaps the area with the prompt "Click here to add a new row". Once the user is done editing one of the rows. An IndexOutOfRangeException is thrown when the grid tries to set the value to the extra row. How can I avoid this?
public
NoteHistoryControl()
{
InitializeComponent();
this
.noteHistoryGridView.DataSource =
new
BindingList<Note>(
this
._noteHistory);
}
private
void
noteHistoryGridView_CellValueChanged(
object
sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
GridViewDataColumn column = e.Column
as
GridViewDataColumn;
if
(column !=
null
&& column.FieldName ==
"Text"
)
{
this
._noteText = e.Value.ToString();
this
.noteHistoryGridView.MasterGridViewTemplate.AllowAddNewRow =
false
;
}
}
private
void
InitializeComponent()
{
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridViewDateTimeColumn gridViewDateTimeColumn1 =
new
Telerik.WinControls.UI.GridViewDateTimeColumn();
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn2 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridSortField gridSortField1 =
new
Telerik.WinControls.UI.GridSortField();
this
.panel1 =
new
System.Windows.Forms.Panel();
this
.noteLabel =
new
System.Windows.Forms.Label();
this
.noteHistoryGridView =
new
Telerik.WinControls.UI.RadGridView();
this
.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(
this
.noteHistoryGridView)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.noteHistoryGridView.MasterGridViewTemplate)).BeginInit();
this
.SuspendLayout();
//
// panel1
//
this
.panel1.Controls.Add(
this
.noteLabel);
this
.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this
.panel1.Location =
new
System.Drawing.Point(0, 0);
this
.panel1.Name =
"panel1"
;
this
.panel1.Padding =
new
System.Windows.Forms.Padding(3);
this
.panel1.Size =
new
System.Drawing.Size(400, 28);
this
.panel1.TabIndex = 0;
//
// noteLabel
//
this
.noteLabel.AutoSize =
true
;
this
.noteLabel.Font =
new
System.Drawing.Font(
"Microsoft Sans Serif"
, 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((
byte
)(0)));
this
.noteLabel.Location =
new
System.Drawing.Point(6, 6);
this
.noteLabel.Margin =
new
System.Windows.Forms.Padding(3);
this
.noteLabel.Name =
"noteLabel"
;
this
.noteLabel.Size =
new
System.Drawing.Size(44, 16);
this
.noteLabel.TabIndex = 1;
this
.noteLabel.Text =
"Notes"
;
//
// noteHistoryGridView
//
this
.noteHistoryGridView.BackColor = System.Drawing.SystemColors.Control;
this
.noteHistoryGridView.Cursor = System.Windows.Forms.Cursors.Default;
this
.noteHistoryGridView.Dock = System.Windows.Forms.DockStyle.Fill;
this
.noteHistoryGridView.Font =
new
System.Drawing.Font(
"Microsoft Sans Serif"
, 8.25F);
this
.noteHistoryGridView.ForeColor = System.Drawing.SystemColors.ControlText;
this
.noteHistoryGridView.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this
.noteHistoryGridView.Location =
new
System.Drawing.Point(0, 28);
//
//
//
this
.noteHistoryGridView.MasterGridViewTemplate.AddNewRowPosition = Telerik.WinControls.UI.PinnedRowPosition.Bottom;
this
.noteHistoryGridView.MasterGridViewTemplate.AllowColumnChooser =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AllowColumnReorder =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AllowDeleteRow =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AllowDragToGroup =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AllowEditRow =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AutoGenerateColumns =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
gridViewTextBoxColumn1.AllowFiltering =
false
;
gridViewTextBoxColumn1.AllowGroup =
false
;
gridViewTextBoxColumn1.AllowHide =
false
;
gridViewTextBoxColumn1.FieldAlias =
"columnNote"
;
gridViewTextBoxColumn1.FieldName =
"Text"
;
gridViewTextBoxColumn1.HeaderText =
"Note"
;
gridViewTextBoxColumn1.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
gridViewTextBoxColumn1.MinWidth = 50;
gridViewTextBoxColumn1.Multiline =
true
;
gridViewTextBoxColumn1.UniqueName =
"columnNote"
;
gridViewTextBoxColumn1.Width = 215;
gridViewTextBoxColumn1.WrapText =
true
;
gridViewDateTimeColumn1.AllowFiltering =
false
;
gridViewDateTimeColumn1.AllowGroup =
false
;
gridViewDateTimeColumn1.AllowHide =
false
;
gridViewDateTimeColumn1.DataType =
typeof
(System.DateTime);
gridViewDateTimeColumn1.FieldAlias =
"EntryDate"
;
gridViewDateTimeColumn1.FieldName =
"EntryDate"
;
gridViewDateTimeColumn1.HeaderText =
"Entry Date"
;
gridViewDateTimeColumn1.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
gridViewDateTimeColumn1.MinWidth = 50;
gridViewDateTimeColumn1.ReadOnly =
true
;
gridViewDateTimeColumn1.UniqueName =
"columnDate"
;
gridViewDateTimeColumn1.Width = 215;
gridViewTextBoxColumn2.AllowFiltering =
false
;
gridViewTextBoxColumn2.AllowGroup =
false
;
gridViewTextBoxColumn2.AllowHide =
false
;
gridViewTextBoxColumn2.FieldAlias =
"EntryUsername"
;
gridViewTextBoxColumn2.FieldName =
"EntryUsername"
;
gridViewTextBoxColumn2.HeaderText =
"Username"
;
gridViewTextBoxColumn2.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
gridViewTextBoxColumn2.MinWidth = 50;
gridViewTextBoxColumn2.ReadOnly =
true
;
gridViewTextBoxColumn2.UniqueName =
"columnUsername"
;
gridViewTextBoxColumn2.Width = 215;
this
.noteHistoryGridView.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn1);
this
.noteHistoryGridView.MasterGridViewTemplate.Columns.Add(gridViewDateTimeColumn1);
this
.noteHistoryGridView.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn2);
this
.noteHistoryGridView.MasterGridViewTemplate.EnableGrouping =
false
;
this
.noteHistoryGridView.MasterGridViewTemplate.ShowRowHeaderColumn =
false
;
gridSortField1.FieldAlias =
"columnDate"
;
gridSortField1.FieldName =
"columnDate"
;
this
.noteHistoryGridView.MasterGridViewTemplate.SortExpressions.Add(gridSortField1);
this
.noteHistoryGridView.Name =
"noteHistoryGridView"
;
this
.noteHistoryGridView.RightToLeft = System.Windows.Forms.RightToLeft.No;
this
.noteHistoryGridView.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;
this
.noteHistoryGridView.ShowGroupPanel =
false
;
this
.noteHistoryGridView.Size =
new
System.Drawing.Size(400, 122);
this
.noteHistoryGridView.TabIndex = 1;
this
.noteHistoryGridView.ThemeName =
"ControlDefault"
;
this
.noteHistoryGridView.CellValueChanged +=
new
Telerik.WinControls.UI.GridViewCellEventHandler(
this
.noteHistoryGridView_CellValueChanged);
//
// NoteHistoryControl
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.Controls.Add(
this
.noteHistoryGridView);
this
.Controls.Add(
this
.panel1);
this
.MinimumSize =
new
System.Drawing.Size(350, 125);
this
.Name =
"NoteHistoryControl"
;
this
.Size =
new
System.Drawing.Size(400, 150);
this
.panel1.ResumeLayout(
false
);
this
.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(
this
.noteHistoryGridView.MasterGridViewTemplate)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.noteHistoryGridView)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}