Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Professional C#, 2nd and 3rd Editions
|
BOOK: Professional C#, 2nd and 3rd Editions
This is the forum to discuss the Wrox book Professional C#, 3rd Edition by Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen; ISBN: 9780764557590
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C#, 2nd and 3rd Editions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2003, 06:32 PM
Authorized User
 
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default For Loop problem with DataSet\DataTable

I have a dataset 'headlines' that contains single DataTable having news headlines and mage information.
Each news may have associated image with it or null if no mage exists for a new.
The dataset has only one DataTable & this DataTable will look like as seen below. Pretty simple.

news_id news_datereleased news_source news_imageurl
------- ------------------ ------------ --------------
2 12-29-03 10:20 AP http://../ap_image1.jpg
5 12-29-03 09:30 Reuters NULL
9 12-28-03 11:30 AFP http://../afp_image2.jpg
8 12-27-03 12:23 BBC NULL
5 12-28-03 11:30 CNN http://../cnn_image2.jpg
7 12-27-03 12:23 AP http://../ap_image2.jpg


Now I have a datagrid which is bound to above table. This grid has an image object
and I want to set it's imageUrl property to above 'news_imageurl' column which works fine.

Now the problem: it is showing missing image object for 2nd and 4th row in the grid.
To solve this problem I need to set the visible property of 2nd & 4th row to false.

solution I panned:

headlines.Tables[0].Columns.Add("news_imagevisibility", System.Type.GetType("System.Boolean"), Boolean.FalseString);

so far good. All images are by default NOT Visible on the grid. so my changed DataTable looks like:
news_id news_datereleased news_source news_imageurl news_imagevisibility
------- ------------------ ------------ -------------- ---------------------
2 12-29-03 10:20 AP http://../ap_image1.jpg false
5 12-29-03 09:30 Reuters NULL false
9 12-28-03 11:30 AFP http://../afp_image2.jpg false
8 12-27-03 12:23 BBC NULL false
5 12-28-03 11:30 CNN http://../cnn_image2.jpg false
7 12-27-03 12:23 AP http://../ap_image2.jpg false

Next step is to change 'news_imagevisibility' of the DataTable as shown below
news_id news_datereleased news_source news_imageurl news_imagevisibility
------- ------------------ ------------ -------------- ---------------------
2 12-29-03 10:20 AP http://../ap_image1.jpg true
5 12-29-03 09:30 Reuters NULL false
9 12-28-03 11:30 AFP http://../afp_image2.jpg true
8 12-27-03 12:23 BBC NULL false
5 12-28-03 11:30 CNN http://../cnn_image2.jpg true
7 12-27-03 12:23 AP http://../ap_image2.jpg true

Then in asp page I will have code like this:
 <asp:DataGrid .......>
    <Columns>
       <asp:TemplateColumn>
    <ItemTemplate>
       <asp:image id='..' runat="server" imageurl='<%# DataBinder.Eval(Container.DataItem,"news_imageurl" )%>'
                                  visible='<%# DataBinder.Eval(Container.DataItem,"news_imagevisi bility")%>' >
    <ItemTemplate>
       </asp:TemplateColumn>
       <asp:TemplateColumn> rest of the news info goes here </asp:TemplateColumn>
    </Columns>
 </asp:DataGrid>

Code that I have written:
//use for loop to change all rows in DataTable having valid image
for(int i=0; i<headlines.Tables[0].Rows.Count; i++)
{
  if ((string)headlines.Tables[0].Rows[i]["ni_turl"].GetType() != Type.GetType("System.DBNull"))
     headlines.Tables[0].Rows[i]["news_imagevisibility"]=Boolean.TrueString;
}

The problem is that above loop fails to update the DataTable since it says the cell is READONLY!!!
Do you you how can I get this working???????

Thanks in Advance
Musa

 
Old December 24th, 2003, 09:46 AM
Authorized User
 
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to KABay
Default

Several thoughts ...

Why not just set news_imageurl to some 'default' image like (?) http://../ap_no_image.jpg that shows the test "Image not available" (or some other appropriate verbal) for those records that have no image available.

Why do you need the news_imagevisibility column in the DataTable. In your ASP code can't you evaluate if the image url is Null and set the visibility of the control then?

Sorry I don't have time to write some test code to confirm my thoughts.

KABay





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rearrange the column order in a datatable/dataset kiran_q8 ASP.NET 2.0 Professional 2 May 3rd, 2007 08:46 AM
Creating DataSet/DataTable from XML hhmoeller XML 4 March 22nd, 2006 10:41 AM
DataTable belongs to another DataSet bmains ADO.NET 2 December 20th, 2005 12:24 AM
For Loop problem with DataSet\DataTable musa BOOK: ASP.NET Website Programming Problem-Design-Solution 2 December 25th, 2003 03:11 AM
For Loop problem with DataSet\DataTable musa BOOK: Beginning ASP.NET 1.0 0 December 23rd, 2003 06:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.