I have quite a complex set of classes which parses an XML file or RSS feeds.
I know that radgrid can bind to an arraylist, so I have this code:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
ArrayList arraylist = new ArrayList();
if (dict != null)
{
foreach (var v in dict)
{
arraylist.Add(new RSSFeedItem(v.Date, v.Description, v.Title, v.Title));
RadGrid1.DataSource = arraylist;
}
}
}
"dict" is a collection of articles in an RSS feed. So for every article inside, I add that article's date, link, description, etc to the array list, and then set the datasource to that arraylist.
On the last line of the code, I check the grid's state and it is containing the items (e.g. first pass in the loop, there is a count of 1, etc). Datasource has a count of 25 which contains the articles.
Also, how can I get the row count programatically?
Thanks
7 Answers, 1 is accepted

I modified my code, like so:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
ArrayList arraylist = new ArrayList();
if (dict != null)
{
foreach (var v in dict)
{
arraylist.Add(new RSSFeedItem(v.Date, v.Description, v.Title, v.Title));
}
RadGrid1.DataSource = arraylist;
}
}
}
Still no luck.
I think that you have one extra closing braket than necessary. Please remove it and use the example from here for reference.
Greetings,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.

I may have accidentally added a bracket in my code above. Problem is, that would be detected by Visual Studio anyway and would stop my code from compiling.
Some facts:
I am binding to an arraylist, yes, but each arraylist holds an object (struct) which has 5 values initialised in its parametered constructor. Is this a problem? I did do a test and I bound a standard arraylist just holding strings. This worked fine.
The datasource and arraylist both have a count of >0.
The grid's column count, however, is 0 though? Can this be right?
Some code samples:
Below, I loop through a collection of rss feed titles (I know the naming conventions arent 100%) and if it equals the title of the clicked radpanel member (I'm using radpanel for representing rss feed titles), then set the url to parse based on the title (so if the title is yahoo, I can get a yahoo rss feed - I have some code which does this). I get the articles into a collection called dict, which is of type Collection<T>.
foreach (string item in Items)
{
if (e.Item.Text == item)
{
// There is a match between the selected node and the item in the list of rss feeds
rssmanager.Url = feedUrl; // Url to parse is the url of the selected node, where it matches with the collection of feed titles (See above)
dict = rssmanager.GetFeed(feedUrl);
RadGrid1_NeedDataSource(this, null);
break;
}
}
I call RadGrid1_NeedDataSource(this, null); which looks like this:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
ArrayList arraylist = new ArrayList();
if (dict != null && dict.Count > 0)
{
foreach (var v in dict)
{
arraylist.Add(new RSSFeedItem(v.Date, v.Description, v.Title, v.Title));
}
}
RadGrid1.DataSource = arraylist;
}
Your suggestion didn't work. Any idea what is wrong? The markup for the grid is very simple:
<telerik:RadGrid ID="RadGrid1" runat="server" Enabled="true" Visible="true" Skin="Default"
onneeddatasource="RadGrid1_NeedDataSource" Width="536px"
GridLines="None">
<MasterTableView>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
</telerik:RadGrid>
I would submit a support ticket but I am not able to as I am just using the trial to evaluate the power of the product (quite impressed so far, just need to get some experience to really push the tools).
Thanks

Try storing the value that you add to the arraylist to a ViewState. And then before binding the arraylist value to the Grid reassign the value for the arraylist from the Viewstate.
CS:
ArrayList arraylist |
if (ViewState["arraylist"] == null) |
{ |
arraylist= new ArrayList(); |
ViewState["arraylist"]=arraylist ; |
} |
else |
{ |
arraylist= (ArrayList)ViewState["arraylist"]; |
if (arraylist!= null) |
{ |
Radgrid1.DataSource=arraylist; |
} |
} |
You can try the following code snippet to get the row count in code behind.
int rownum= RadGrid1.MasterTableView.Items.Count; |
Regards
Princy.

Princy,
No luck with your code. The problem is it does not bind data <> grid on first pass, so I wrote the below code:
/* if (ViewState["arraylist"] == null)
{
ViewState["arraylist"] = arraylist;
this.RadGrid1.DataSource = ViewState["arraylist"];
}
// viewstate is populated
else
{
arraylist = (ArrayList)ViewState["arraylist"];
if (arraylist != null)
{
this.RadGrid1.DataSource = ViewState["arraylist"];
}
}
*/
Above this branching, I add the contents of the a site's rss items from a collection to the same arraylist (as arraylist can be bound to the grid).
I can actually see the contents of the grid, as the data and grid bind together successfully. See the below screenshot:
URL=http://img216.imageshack.us/my.php?image=datagridissue.png][IMG]http://img216.imageshack.us/img216/3954/datagridissue.th.png[/IMG][/URL]
But still no luck. What gives?
Thanks

I had set
AutoGenerateColumns= "false"
When I set it to true the data showed up.

In order to show data in grid you need to either set the AutoGenerateColumns to 'True' (which automatically add a column for every field of the datasource to which the tableview is bound) or add columns to the columns collection. The latter can be done using Columns property (directly in aspx) or explicitly creating the columns from code behind and adding to column collection of GridTableView.
The following code shows adding columns directly from aspx fitted with the DataTable fields.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid2"
AutoGenerateColumns
=
"false"
runat
=
"server"
GridLines
=
"None"
OnNeedDataSource
=
"RadGrid2_NeedDataSource"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"UserID"
HeaderText
=
"UserID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"status"
HeaderText
=
"status"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
//populate grid using DataSet
protected
void
RadGrid2_NeedDataSource(
object
source, GridNeedDataSourceEventArgs e)
{
DataSet dataset =
new
DataSet();
DataTable dataTable =
new
DataTable();
dataTable.TableName =
"Users"
;
DataColumn colString =
new
DataColumn(
"UserID"
);
colString.DataType = System.Type.GetType(
"System.String"
);
dataTable.Columns.Add(colString);
DataColumn colString1 =
new
DataColumn(
"status"
);
colString1.DataType = System.Type.GetType(
"System.Boolean"
);
dataTable.Columns.Add(colString1);
dataset.Tables.Add(dataTable);
dataTable.Rows.Add(
new
object
[] {
"1"
,
false
});
dataTable.Rows.Add(
new
object
[] {
"2"
,
false
});
RadGrid2.DataSource = dataset;
}
Thanks,
Princy.