How can I get the values from array and pass them one by one to the datagrid?

  Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

Hello people,

 I am developing web application using csharp on .net Framework 1.1 . I got the code from this forum. (www.asp.net).  I have an array where I should get  values and pass them one by one to the datagrid one by one.  How can I write a loop or something that will passing a values from  index  0 to 3.At the moment it is just getting one value from the array.  The csharp code is below.

It is getting let say 2000A writes on all the rows. But I want each row with a different value from array

 
                      int row=0;
			
			  string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
		      
			
							
			if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
			{
				for (row =0; row<2; row++)

				{
					//e.Item.Cells[2].Text = items[row++];
					e.Item.Cells[2].Text = items[row++];
			
				}

			}
			if (e.Item.ItemType == ListItemType.Footer)
			{

			}
 



0
Mobzam
3/2/2009 4:19:36 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

12 Replies
595 Views

Similar Articles

[PageSpeed] 12

 

you can iterate through the array using a foreach.

 

foreach (string s in items)

{

// do something here 

}

 

 

A small example of this:

 

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };

        foreach (string s in items)
        {
            Response.Write(s + "<br />");
        }
    }
 
Larry Dechent - Sampson Coatings

www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
0
ldechent
3/2/2009 4:48:08 PM

 string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
        foreach (string item in items)
        {
            //  Pass each item like this.
        }

0
bendJoe
3/2/2009 4:48:25 PM

 Hello bendJoe and ldechent,
I have tried what you have suggested by I am still having the following problems;
1 First it is not working  as expected, it is just getting one value for all rows,
    The code I am using as follows:
                            

            int row=0;
                 string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };   
                foreach (string item in items)
                {
                    e.Item.Cells[2].Text = items[row++];
 
                }
2. Second  problem it is complaining that  item is not  being used. How can I solve this problem.
       foreach (string item in items)

0
Mobzam
3/3/2009 6:50:13 AM

statement below updated per discussion in subsequent posts: 

Change

e.Item.Cells[2].Text = items[row++];

to

e.Item.Cells[2].Text += item + "<br />";


Larry Dechent - Sampson Coatings

www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
0
ldechent
3/3/2009 12:33:22 PM
I have done what ldechent have suggested, but it is just picking 2000D which is the last 
item in an error. Where I am going wrong? How can I solve this?
0
Mobzam
3/3/2009 1:49:20 PM

You didn't do anything wrong and I am sorry that I didn't spot this when writing my previous reply.

Notice the = sign, it should be +=

Right now it is writing each entry but the second iteration overwrites the first, the third iteration overwrites the second, etc.

e.Item.Cells[2].Text += item;

 

Often I'll have

something.InnerHtml +=     or      something.Text +=

in a loop (or several)

and up somewhere prior to the loop I have

something.InnerHtml = ""   or     something.Text = ""

to clear everything out each time the method containing the loop is used.


Larry Dechent - Sampson Coatings

www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
0
ldechent
3/3/2009 2:15:40 PM

string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
for (int row = 0; row < items.Length; row++)
{
  e.Item.Cells[2].Text = items.GetValue(row).ToString();
}


SSN
Please remember to click "Mark as Answer" on the post that helps you.
0
suthish
3/3/2009 2:28:54 PM
Hello ldechent and suthish nair,
 I have tried both of your suggestions or solutions but I am not getting the way I want it to be.
When I use the ldechent's solution  I am  getting following results.

2000A2000B2000C2000D
2000A2000B2000C2000D
2000A2000B2000C2000D
2000A2000B2000C2000D


But I want is as follows;
2000A
2000B
2000C
2000D

I have tried to Clear the item before it writes , but I am getting
the same results.

While when I use suthish nair solution. I am getting the following results.
2000D
2000D
2000D
2000D


How can I solve this problem???

0
Mobzam
3/3/2009 3:19:01 PM

e.Item.Cells[2].Text = item + "<br />";

The above will add line breaks.

However, what you posted indicates you have something else that is also iterating through the array (and doing so four times, so I assume it is going through the same array).

This isn't present in my model.  Would you like me to post a complete code sample?


Larry Dechent - Sampson Coatings

www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
0
ldechent
3/3/2009 3:38:50 PM

 Please post your some code. However, I have included my code below
 

              private void grdDg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{

			e.Item.Cells[2].Text="";
 					
	   	        string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
			
				
										
			if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
			{
	
		             foreach (string item in items)
			     {
			    e.Item.Cells[2].Text += item;
   	
		             }
				

			}

			if (e.Item.ItemType == ListItemType.Footer)
			
                        {


	                }
 			



		}
 
0
Mobzam
3/4/2009 6:07:45 AM

 Can some help please

0
Mobzam
3/4/2009 3:15:00 PM

Hi Mobzam,

Try this:

protected void grdDg1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.Item.ItemIndex < items.Length)
                e.Item.Cells[2].Text += items.GetValue(e.Item.ItemIndex);
        }
    }

Thanks,


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Qin
3/5/2009 8:42:06 AM
Reply:

Similar Artilces:

can someone tell me how I can pass my concinated values through my where clause one by one???
can someone tell me how I can pass my concinated values through my where clause one by one intead of the entire string that is sepreated by commas which wont work? concianted values (ex. 1,2,3,4)   I need to run each value in teh where clasue instead of (1,2,3,4)  Dim MyConn As SqlConnection Dim myComm1 As SqlCommand Dim myAdpap As SqlDataAdapter Dim myTable As DataTable = New DataTable MyConn = New SqlConnection MyConn.ConnectionString = ConfigurationManager.ConnectionStrings("QuoteSystemConnectionString&quo...

Trying to pass values from one control to another control
I have 2 controls on my page. CaseList and Email Reviwer. CaseList contain a datagrid that when the button (5th column) is clicked 2 values should be passed to the email revier control. In the email Reviwer Control I have declared: I have: public void sendCaseIdCatId(int catid,int caseid) { ViewState["EmailReviewerCategoryId"] = catid; ViewState["EmailReviewerCaseid"] = caseid; } in my EmailReviwer control   I tried: //Display the email reviewer panel EmailCaseReviewer.Visible=true; string strcatid = e.Item.Cells[1].Text.ToString(); string strcaseid = e.Item.Cells[0].Text.T...

Passing value from one control to another and inserting that value in a table
Hey all, sorry if the subject is confusing or has already been covered, but I have (obviously) a problem (at least one).This is using:Visual Studio 2005ASP.NET 2.0SQL Server 2005 ExpressVisual Basic I'm building a CRM system for my company and have run into a snag when attempting to add new customer records for a given client. The first thing that any CSR (or admin) must do upon accessing the CRM system is choose which client's customers they are actually working with (we have multiple clients and are ultimately responsible for providing customer service to the clients' end ...

Pass value from one User Control to another User Control
I have tried to pass value from one user control to another user control. Because of Page Auto Postback, Contrls are load before page is loaded. So I get the value in 2nd UC after one more post back operation. I have visited many suggessions but I cant solve this. Is anyone can help me to solve this. Thank U mfhossain@gmail.com Muhmmad Fakhrul HossainEmail: mfhossain@gmail.comPhone: +88 01715 111512Web: http://www.mfhossain.info You'd have to do that through the parent probably in it's Page_Load event handler.   userControl1.publicProperty = userControl2.publicProperty;  ...

How to pass control values from one ascx user-control to another???
Our site is set up using a primary default aspx page that is used as the "base page" for everything. On this page, we dynmaically place various user controls at run-time. This gives us a functionality similar to using frames where the outer-frame actually just sits there while various webfiles are loaded inside of it. Works great except for one thing... trying to figure out the best way to pass control names/values accross user controls. When the same usercontrol is posting back to itself this is a non issue. So far most of our functionality has worked well using the local...

How to pass a array of values from one aspx page to other (string array)
 Hi everyone I have a array of strings in a page which is a string array i need to pass all the values of that array to other page while i am redirecting to the new page i need to have acces to that page can anyone please help me out in this issue  Store the array in a session in the first page and access it from the second.Mark replies as answers if they helped you solve the problem. There are numerous ways: !) Store it in data cache 2)Store it in Session 3)If the data is in a datasource, you could have it posted to the new page and that page could have access to the previ...

Error in JavaScript To Check All CheckBox In only in ONe DataGrid When Having More than one DataGrid in One Page
Hello Guys, I am having total 8 DataGrid on a Page. I want to apply javascript so that if i click on CheckBox in Header Row Then All CheckBox inside ItemTemplate should be Checked automatically. I applied the following javascript to do so: function CheckAll( checkAllBox ) { var frm = document.form1; var ChkState=checkAllBox.checked; for(i=0;i { e=frm.elements[i]; if(e.type=='checkbox' && e.name.indexOf('chkView') != -1) ...

How get one value from one column from sqldatasource?
Hi, a few days i am trying to get one value from one column from database and put in one local variable (INT)........   i trying this:protected void SqlDataSource1_Load(object sender, EventArgs e)protected void SqlDataSource1_Load(object sender, EventArgs e) {  Int32 status;  SqlDataSource1.SelectCommand= "SELECT status FROM users WHERE Username='" + Membership.GetUser() + "'";status = SqlDataSource1.Select(DataSourceSelectArguments.Empty);   } "status" is the variable in question....   anyone can help me?  tan...

Ways to get a mapping of values versus keys in a hash where there is one-to-one mapping between the keys and the values !
------=_Part_6250_27861417.1227632496175 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all, I recently had one requirement where I needed to get the key in a hash on the basis of supplied value. There is one additional information that there is one to one mapping between the keys and the values. I was able to do that by defining a extra array and hash variables as shown in the program below but I feel this approach will not work for all scenarios like :- 1. When the value of a particular key is undef in the ori...

want to call one c#.net function in one user control from another user control
hi i have two user controls info.ascx and personal.ascx i have a function like in info.ascx i have a function like showdetails() {   } now i want to access this function from personal.ascx can i do this ? thanks in advance. shwetaThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path  you should better create a class where you will define the "functions" that you need in both files. declare "static" the function and call it like nameofclass.nameoffunction In a web project you can...

Pass values from one user control to a user control on a different aspx page
Hello, I have an aspx page titled Search.aspx. Within this page, I have a user control titled Search.ascx. I want a user to input search terms, then on the click of a button, I want to pass the values to another page (SearchResults.aspx)....which will then display the results in a user control. Can someone please give me some suggestions on how to do this? Thanks a lot! Hello, what you can do is: inside ur usercontrol, create a method that returns a dataset or datareader. that is, since u are searching using the usercontrol, then the result of ur search will be return...

Can not put more than one ModalPopup control in one page?
I used the Version 1.0.10301.0 Ajax Control Toolkit, but when I put two ModalPopup control in one page ,neither of them work.I have used several ModalPopup control in one page with the 1.0.10123 Toolkit ,and it works fine! Why the new version has this problem?  up ~ Is there anyone can help me ? up~ How about a code example?  The Last time I tried todo this I had problems where one of the popups would always cause a full postback and I could not figure a way to get both popups to post asynchronously, so I would be interested to see if your problem is similar. -Steve&nb...

One to Many to One to One
I have a kinda small problem; if someone could help me that' would be niceTo keep things simplier, allow me to give the following exampleI have two tables one-to-many relationship,table1: itemtable2: itemhistoryitem has many itemhistorybasically i want retrieve all the records in 'item' sorted by 'name', along with ONE "latest" itemhistory" (date). If there aren't any historyhistory for a particular 'item', just allow null values. egcreate table #item(itemid int, name varchar(10))create table #itemhistory(itemhistoryid int, itemid int, date...

One-to-One or Many-to-One?
 Hi Friends,I've made huge database for Learning Management Systems which has around 30 - 40 tables.I was checking all the relationships between tables and everything seems fine and well defined. But, it's 4:22 AM here and I'm bit sleepy but I was thinking which of the following relationship is true between the following two tables:Table1: tbl_users (PK username, password, name, surname, email, address1, address2, phNumb)Table2: tbl_testAttempts (PK Id, username, testId, attempt, timeStart, timeEnd)As you see, the Primary key of tbl_users is foreign key in tbl_testAttempts. ...

Web resources about - How can I get the values from array and pass them one by one to the datagrid? - asp.net.presentation-controls

Want to preview our new DataGrid for Xamarin.Forms?
tl;dr Zumero.DataGrid is a Xamarin.Forms control for displaying data in rows and columns. If you would be interested in testing and previewing ...

Mad King DataGrid
This post is about scrolling in WPF and the egocentric DataGrid control. I’ll give the project background but if all you’re interested in is ...

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 295 ...

Microsoft Silverlight - Wikipedia, the free encyclopedia
Microsoft Silverlight is an application framework for writing and running rich Internet applications , with features and purposes similar to ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Casaba Security » Products » X5s Testing Tool For Finding XSS Vulnerabilities
Security analysis, consulting, development and testing partners who understand your business strategy.

CodeGuru - Microsoft developers related ideas, articles, tips, tricks, comments, downloads, and so much ...
CodeGuru is where developers can come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and so much more related ...

Caliburn Micro: WPF, Silverlight, WP7 and WinRT/Metro made easy.
A small, yet powerful framework designed for WPF, Silverlight and WP7. It implements a variety of UI patterns for solving real-world problems. ...

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 11 Jul 2012

license-discuss@opensource.org: 11636: first draft of license proliferation committee report
FYI,the first draft report of the OSI's License Proliferation Committee.To join the OSI's license proliferation discuss email list, send anemail ...

Resources last updated: 12/30/2015 1:43:33 PM