Hi, I try to add new row to radGrid by this code (Add button)
After dgdPart.Rebind(); code step into
I try to debug and see while e.Item.ItemIndex is 0 and the dgdPart.Items.Count is 0 at line "//Error line" so I got error "Index was out of range. Must be non-negative and less than the size of the collection."
I also found the dgdPart.Items.Count is 0 only in dgdPart_ItemDataBound, If I skip the erorr line above and continue until finish dgdPart_ItemDataBound, It comeback to dgdPart.Items.Count = 1
Could you suggest me to solve this problem? Thanks!!
Below is my aspx code for this radGrid
PartDt.Rows.InsertAt(dr, 0);
dgdPart.CurrentPageIndex = 0;
dgdPart.DataSource = PartDt;
dgdPart.DataBind();
dgdPart.MasterTableView.Items[0].Edit =
true
;
dgdPart.Rebind();
After dgdPart.Rebind(); code step into
protected
void
dgdPart_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item.IsInEditMode && e.Item
is
GridEditFormItem)
{
if
(e.Item.ItemIndex > -1)
{
RadComboBox cmbPart = (RadComboBox)dgdPart.Items[e.Item.ItemIndex].FindControl(
"radcmbPart"
);
//Error line
....
}
I try to debug and see while e.Item.ItemIndex is 0 and the dgdPart.Items.Count is 0 at line "//Error line" so I got error "Index was out of range. Must be non-negative and less than the size of the collection."
I also found the dgdPart.Items.Count is 0 only in dgdPart_ItemDataBound, If I skip the erorr line above and continue until finish dgdPart_ItemDataBound, It comeback to dgdPart.Items.Count = 1
Could you suggest me to solve this problem? Thanks!!
Below is my aspx code for this radGrid
<
telerik:RadGrid
ID
=
"dgdPart"
runat
=
"server"
AllowPaging
=
"True"
CellSpacing
=
"0"
GridLines
=
"None"
AutoGenerateColumns
=
"False"
OnEditCommand
=
"dgdPart_EditCommand"
OnNeedDataSource
=
"dgdPart_NeedDataSource"
OnCancelCommand
=
"dgdPart_CancelCommand"
OnItemDataBound
=
"dgdPart_ItemDataBound"
OnUpdateCommand
=
"dgdPart_UpdateCommand"
>
<
ClientSettings
>
<
Selecting
CellSelectionMode
=
"None"
>
</
Selecting
>
</
ClientSettings
>
<
MasterTableView
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
>
</
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridTemplateColumn
FilterControlAltText
=
"Filter CheckBoxTemplateColumn column"
UniqueName
=
"CheckBoxTemplateColumn"
>
<
ItemTemplate
>
<
asp:CheckBox
ID
=
"chkToDelete"
runat
=
"server"
/>
</
ItemTemplate
>
<
ItemStyle
HorizontalAlign
=
"Center"
VerticalAlign
=
"Top"
Width
=
"20px"
/>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"t_name"
FilterControlAltText
=
"Filter column column"
HeaderText
=
"อะไหล่"
UniqueName
=
"column"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"life_time"
FilterControlAltText
=
"Filter column1 column"
HeaderText
=
"อายุการใช้งาน"
UniqueName
=
"column1"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"start_date"
FilterControlAltText
=
"Filter column2 column"
HeaderText
=
"วันเริ่มต้นใช้งาน"
UniqueName
=
"column2"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"expire_date"
FilterControlAltText
=
"Filter column3 column"
HeaderText
=
"วันสิ้นสุดใช้งาน"
UniqueName
=
"column3"
>
</
telerik:GridBoundColumn
>
<
telerik:GridEditCommandColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
EditColumn
uniquename
=
"edit_data_form"
/>
<
FormTemplate
>
<
table
>
<
tr
>
<
td
style
=
"width:100px;height:20px"
>
อะไหล่ :
</
td
>
<
td
>
<
telerik:RadComboBox
ID
=
"radcmbPart"
runat
=
"server"
Width
=
"300px"
></
telerik:RadComboBox
>
</
td
>
</
tr
>
<
tr
>
<
td
style
=
"width:100px;height:20px"
>
อายุการใช้งาน :
</
td
>
<
td
>
<
telerik:RadNumericTextBox
ID
=
"radtxtLiftTime"
runat
=
"server"
Text='<%# Bind("life_time") %>' Width="100px"></
telerik:RadNumericTextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
style
=
"width:100px;height:20px"
>
วันเริ่มต้นใช้งาน :
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtPartStartDate"
Text='<%# Bind("start_date") %>' runat="server" ></
asp:TextBox
>
<
asp:CalendarExtender
ID
=
"calendarPartStartDate"
Format
=
"dd/MM/yyyy"
runat
=
"server"
TargetControlID
=
"txtPartStartDate"
></
asp:CalendarExtender
>
</
td
>
</
tr
>
</
tr
>
<
tr
>
<
td
style
=
"width:100px;height:20px"
>
วันสิ้นสุดใช้งาน :
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtPartExpireDate"
Text='<%# Bind("expire_date") %>' runat="server" ></
asp:TextBox
>
<
asp:CalendarExtender
ID
=
"calendarPartExpireDate"
Format
=
"dd/MM/yyyy"
runat
=
"server"
TargetControlID
=
"txtPartExpireDate"
></
asp:CalendarExtender
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
<
td
>
<
asp:Button
ID
=
"btnUpdate"
CommandName
=
"Update"
Text
=
"Update"
runat
=
"server"
/> <
asp:Button
ID
=
"btnCancel"
CommandName
=
"Cancel"
Text
=
"Cancel"
runat
=
"server"
/>
</
td
>
</
tr
>
</
table
>
</
FormTemplate
>
<
PopUpSettings
ScrollBars
=
"None"
/>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
>
</
FilterMenu
>
</
telerik:RadGrid
>