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

Excel-Like RadGrid with Paging

2 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Revathi Panneerselvam
Top achievements
Rank 1
Revathi Panneerselvam asked on 01 Oct 2010, 09:20 AM
The Excel-Like Radgrid code that given by telerik is very useful. I am using the code excellikegrid-updated.zip in my application. It is working fine for the first set of records.

I am using virtual paging in that Excel-like RadGrid solution. For first page, the records displaying as like excel editable mode and we are able to add/Update data in those cells. But when we go to second page using pagination it throws an exception follows. 
"Specified argument was out of the range of valid values.
Parameter name: index".

at the line   "TextBox textBox = (item[column.UniqueName].Controls[0]) as TextBox;"    Inside pre-render event of RadGrid.

At the first time(page load), After "Need Datasource" event, the event "Data Binding" firing and sets all the records in the grid to edit mode.

protected void radGridProducts_DataBinding(object sender, EventArgs e)
   {
       DataTable currentProducts = _presenter.GetCurrentDataTable();
       for (int i = 0; i < currentProducts.Rows.Count; i++)
       //for (int i = 0; i < radGridProducts.VirtualItemCount; i++)
       {
           radGridProducts.EditIndexes.Add(i);
       }
   }

At the time of postback for Next page, after the event "Need Datasource", the "radGridProducts_DataBinding" event is not firing and it goes to pre-render event; no controls in any item of grig and displays the above error message.

I have tried to set all grid items to editable mode in pre-render event iteself as follows.

protected void radGridProducts_PreRender(object sender, EventArgs e)
       {
           //Added for Test purpose
           DataTable currentProducts = _presenter.GetCurrentDataTable();
           for (int i = 0; i < currentProducts.Rows.Count; i++)           
           {
               radGridProducts.EditIndexes.Add(i);
           //Added for Test purpose
           radGridProducts.Attributes.Add("onkeydown", "onKeyDown(this,event);");
           int itemsCount = 0;
           int columnsCount = 0;
           StringBuilder builder = new StringBuilder();
           // Attach the event handlers to the client side events of the TextBoxes. 
           foreach (GridDataItem item in radGridProducts.MasterTableView.Items)
           {
               if (item is GridDataItem)
               {
                   columnsCount = 0;
                   for (int i = 2; i < radGridProducts.MasterTableView.RenderColumns.Length; i++)
                   {
                       GridColumn column = radGridProducts.MasterTableView.RenderColumns[i];
                       //if (item[column.UniqueName].Controls.Count != 0)
                       //{
                           TextBox textBox = (item[column.UniqueName].Controls[0]) as TextBox;
                           if (textBox != null)
                           {
                               textBox.Attributes.Add("ondblclick", "cellDoubleClickFunction('" + textBox.ClientID + "');");
                               textBox.Attributes.Add("onclick", "cellClick('" + textBox.ClientID + "');");
                           }
                           if (i == 2 && !string.IsNullOrEmpty(textBox.Text))
                           {
                               textBox.ReadOnly = true;
                               textBox.Attributes.Add("class", "readOnly");
                           }
                       //}
                       columnsCount++;
                   }
                   itemsCount++;
               }
           }
           RadScriptManager.RegisterStartupScript(Page, Page.GetType(), "init", "colls = " + columnsCount + ";rows=" + itemsCount + ";", true);
                  
       }


But still, it throws the same "Index out of bound" exception while getting the TextBox control. 

Any solution for this Issue? 
Thanks in Advance.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Oct 2010, 11:02 AM
Hello Revathi,

Since the column is rendered as TextBox in edit mode, you need to loops through all edit rows to access TextBox for that column. Check out the following code snippet.

C#:
protected void radGridProducts_PreRender(object sender, EventArgs e)
       {
         //. . . . . . . . . . . . . . . . . .
 
          foreach (GridEditableItem editItem in radGridProducts.MasterTableView.GetItems(GridItemType.EditItem)) //loops through all edited rows
           {
                for (int i = 2; i < radGridProducts.MasterTableView.RenderColumns.Length; i++)
                   {
                       GridColumn column = radGridProducts.MasterTableView.RenderColumns[i];
                       TextBox textBox = (editItem[column.UniqueName].Controls[0]) as TextBox;
                        . . . . . . . . . . . .
                   
           
         }

Thanks,
Princy.
0
Revathi Panneerselvam
Top achievements
Rank 1
answered on 01 Oct 2010, 02:06 PM
Thanks Princy,

You are right. But I need all grid items should be editable mode. so that we are setting all the grid tems to be in editable mode by using following code.

protected void radGridProducts_DataBinding(object sender, EventArgs e)
        {
            DataTable currentProducts = _presenter.GetCurrentDataTable();
            for (int i = 0; i < currentProducts.Rows.Count; i++)  
            {
                radGridProducts.EditIndexes.Add(i);
            }
            IsAllEditMode = true;
        }

But, at second time post back of the page, the above event is not firing.

Now, I have added a line in pre-render event.

protected void radGridProducts_PreRender(object sender, EventArgs e)
        {            
              if (!IsAllEditMode)
                radGridProducts.DataBind();
//....

Now it is firing DataBinding event and working.
Thanks!!
Tags
Grid
Asked by
Revathi Panneerselvam
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Revathi Panneerselvam
Top achievements
Rank 1
Share this question
or