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

GridTemplateColumn Problem

5 Answers 154 Views
Grid
This is a migrated thread and some comments may be shown as answers.
alsi
Top achievements
Rank 1
alsi asked on 24 Aug 2008, 01:05 PM
Dear All!

I have a big problem with the GridTemplateColumn. I've created grid in the PageInit at code-behind. Everything works fine except one thing.
I have a Template Column with different running number. Means every time I need to create different pieces of  (Template) Columns. At first 5 pieces, after a button postback 2 pieces (depends on _numberOfMD), etc. :

protected void Page_Init(object sender, EventArgs e)
{
    RadGrid _grdEval = new RadGrid();
    ....
    for (int i = 0; i < _numberOfMD; i++)
    {
      GridTemplateColumn tmplColumn = new GridTemplateColumn();
      tmplColumn.HeaderText = "ESY<BR />OK";
      tmplColumn.UniqueName = "EasyChkTempColumn" + i.ToString();
       tmplColumn.ItemTemplate = new MDTemplate("MD");
       tmplColumn.AllowFiltering = false;
       this._grdEval.Columns.Add(tmplColumn);
    }
}

But the error: At the first load (not postback) five columns created successfully and after postback (with button press) if I want to create only 2 columns (the _numberOfMD will be 2 instead of 5), the following exception thrown:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

How can I do that every time
with different _numberOfMD the Grid works well without exception and create and bind number of columns as I want.

thanks

5 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 25 Aug 2008, 03:10 PM
Hello alsi,

The reason you are getting this error is that Page_Init is the first step in the page lifecycle. As you need to define your RadGrid inside the Page_Init event in code-behind, this event is fired every time before any other event on the page. Therefore you are enforced to change RadGrid's structure once it has been set in Page_Init. Therefore you are getting an invalid index exception. Changing RadGrid's struncture in this way is not an adviseable approach. The safest approach would be to make sure all of your columns are created and, if needed, some of them conditionally hidden.

Regards,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
alsi
Top achievements
Rank 1
answered on 26 Aug 2008, 08:51 AM
Thanks For Ur answer Veli!

My last question about this: what's happening if I bind the TemplateColumn checkbox and textbox field with dataset data and the number of columns in the dataset less than the number of TemplateColumns.
For exampl: In the Page_Init i've created 30 TemplateColumns but the dataset only contains 12 columns which I can bind (so 18 column will be empty and I want to hide). Will it thrown an error or not?
0
Veli
Telerik team
answered on 26 Aug 2008, 09:37 AM
Hi alsi,

If your checkbox and textbox controls bind to properties (datafields) that are not currently in the source, you will, indeed, get an exception. You will need to take this into consideration too. The best approach would be to not bind them declaratively (using Bind() method), but to set their values manually in the ItemDataBound event based on the columns returned.

Regards,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
alsi
Top achievements
Rank 1
answered on 26 Aug 2008, 12:49 PM
And in which Event can I hide the don't needed columns? And how?
My columns unique name create in the following way:

tmplColumn.UniqueName = "EasyChkTempColumn" + i.ToString();

so if I will have 10 Columns how can I hide the last 3 one?

thx
0
alsi
Top achievements
Rank 1
answered on 26 Aug 2008, 02:35 PM
Ok, I have found. ItemDataBound event..

cu.
Tags
Grid
Asked by
alsi
Top achievements
Rank 1
Answers by
Veli
Telerik team
alsi
Top achievements
Rank 1
Share this question
or