Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
30
Index out of range Exception on deleting a row
posted

My code binds WebDataGrid to DataTable. The columns and rows are being read from Database to DataTable and columns are being added to WebDataGrid using BoundDataField. 

Any row but the last being deleting results in index out of range exception. I can delete only last row without errors.

If I commment m_dictionaryTableGrid.DataBind(); in rowDeleting event, everything works fine.

C# Code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["dictionaryTableKey"] != null)
{
//............
m_dictionaryTableItems = m_adapter.GetdictionaryTableItems(new Guid(Request.QueryString["dictionaryTableKey"].ToString()));
foreach (DataColumn drColumn in m_dictionaryTableItems.Columns)
{
var bField = new BoundDataField(true);
bField.Key = drColumn.ColumnName;
bField.Header.Text = drColumn.ColumnName;
bField.DataFieldName = drColumn.ColumnName;
m_dictionaryTableGrid.Columns.Add(bField);
}
}
//new translation table
else
{
m_navigator.BannerLabel = GetGlobalResourceObject(AdminResources, "EditdictionaryTableItem_CreatedictionaryTable").ToString();
AddNewdictionaryTable();
}
m_dictionaryTableGrid.DataSource = m_dictionaryTableItems;
m_dictionaryTableGrid.DataBind();
}
else
{
UpdateDictionaryTableFromDataGrid();
}
}

private void UpdateDictionaryTableFromDataGrid()
{
if (m_dictionaryTableItems == null)
{
m_dictionaryTableItems = new DataTable(DictionaryTableItems);
foreach (GridField item in m_dictionaryTableGrid.Columns)
{
m_dictionaryTableItems.Columns.Add(item.Header.Text);
}
foreach (GridRecord item in m_dictionaryTableGrid.Rows)
{
DataRow dr = m_dictionaryTableItems.NewRow();
for (int index = 0; index < m_dictionaryTableGrid.Columns.Count; index++)
{
dr[index] = item.Items[index].Text;
}
m_dictionaryTableItems.Rows.Add(dr);
}
}
}

protected void m_dictionaryTableGrid_RowsDeleting(object sender, Infragistics.Web.UI.GridControls.RowDeletingEventArgs e)
{
m_resultsBox.Text = string.Empty;
if (m_dictionaryTableItems.Rows.Count <= 1)
{
m_resultsBox.Text = GetGlobalResourceObject(AdminResources, "EditdictionaryTableItem_TableShouldHaveAtleastOneRow").ToString();
return;
}
else
{
m_dictionaryTableItems.Rows[e.Row.Index].Delete();
m_dictionaryTableGrid.DataSource = m_dictionaryTableItems;
m_dictionaryTableGrid.DataBind();
m_dictionaryTableGrid.RequestFullAsyncRender();
}
}

Css:

<ig:WebDataGrid ID="m_translationTableGrid" runat="server" Height="200px" Width="100%"
AutoGenerateColumns="False" AltItemCssClass="WebDataGridAlternateItem"
EnableDataViewState="true" CssClass="DataGrid" HeaderCaptionCssClass="WebDataGridHeader"
ItemCssClass="WebDataGridItem" BorderStyle='Solid' Font-Names='Verdana'
Font-Size="8pt" BorderColor="Black" BorderWidth="1px" EnableAjax="False"
onrowadding="m_translationTableGrid_RowAdding" onrowsdeleting="m_translationTableGrid_RowsDeleting"
onrowupdating="m_translationTableGrid_RowUpdating"
<Behaviors>
<ig:Activation Enabled="True"></ig:Activation>
<ig:ColumnResizing Enabled="true" />
<ig:EditingCore AutoCRUD="False">
<EditingClientEvents RowsDeleting="m_deletingRow_EventHandler">
</EditingClientEvents>
<Behaviors>
<ig:CellEditing Enabled="true">
<EditModeActions EnableF2="false" EnableOnActive="true" MouseClick="Single" EnableOnKeyPress="false" />
</ig:CellEditing>
<ig:RowAdding Alignment="bottom" AddNewRowCssClass="WebDataGridAddNewItem" EditCellCssClass="WebDataGridAddNewItem" AddNewRowSelectorImageCssClass="WebDataGridAddNewRowSelectorImage">
<EditModeActions EnableF2="false" EnableOnActive="true" MouseClick="Single" />
</ig:RowAdding>
<ig:RowDeleting />
</Behaviors>
</ig:EditingCore>
<ig:Selection CellClickAction="Row" RowSelectType="Multiple" ColumnSelectType="Single" SelectedCellCssClass="DataGridSelectedItem" SelectedRowSelectorCssClass="WebDataGridActiveRowSelectorImage">
<SelectionClientEvents ColumnSelectionChanged="m_dictionaryTableGrid_ColumnSelectionChanged" />
</ig:Selection>
<ig:RowSelectors RowNumbering="True" Enabled="true" RowSelectorCssClass="WebDataGridRowSelector" HeaderRowSelectorCssClass="WebDataGridHeader"/>
</Behaviors>
<EditorProviders>
<ig:TextEditorProvider ID="wdg_TextEditor"></ig:TextEditorProvider>
</EditorProviders>
</ig:WebDataGrid>

Parents
No Data
Reply
  • 740
    Offline posted

    Hello Vijay,

    After investigating this further, I determined that the exception has been thrown because the current row has been deleted form the database but not from the grid collection. What I can suggest is disabling the dataViewState property of the grid, setting dataKeyFields and binding the data to the grid on every postback.

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                . . .

            }

            dataSource = GetDataSource();

            m_dictionaryTableGrid.DataSource = GetDataSource();

            m_dictionaryTableGrid.DataBind();

            m_dictionaryTableGrid.DataKeyFields = "ID";

        }

     

    And the deleteRows method could look as the following:

    protected void m_dictionaryTableGrid_RowsDeleting(object sender, RowDeletingEventArgs e)

        {

            dataSource = GetDataSource();

            dataSource.Rows[e.Row.Index].Delete();

            m_dictionaryTableGrid.Rows.Clear();

     

            m_dictionaryTableGrid.ClearDataSource();

            m_dictionaryTableGrid.DataSource = dataSource;

            m_dictionaryTableGrid.DataBind();

            m_dictionaryTableGrid.RequestFullAsyncRender();

     

        }

    Below I am attaching a sample, demonstrating the described behavior. Please test it on your side and let me know if you need any further information regarding this matter.

    Regards,
    Monika Kirkova,
    Infragistics

    0020.WebDataGridDeleteRows.zip

Children
No Data