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 |
![]() |
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 |
![]() |
string[] items = new string[] { "2000A", "2000B", "2000C", "2000D" };
foreach (string item in items)
{
// Pass each item like this.
}
![]() |
0 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
Can some help please
![]() |
0 |
![]() |
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 |
![]() |