change Container.DataItemIndex


How can a change the idex number??
I want it to show for every other row So     1 Row shows number
                                                                      2nd row doesn't show number
                                                                       3rd row shows number 2

ect??

Any ideas please??
Thanks,


<asp:TemplateField>
 <ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
bailey1200Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaveJellisonCommented:
Hello,

You're definitely on the right track. You may not be aware of the AlternatingItemTemplate feature which allows you to do things differently every other row. Using that in combination with your approach will get you your desired results.

Like so...




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormGrid.aspx.cs" Inherits="WebTestBed.FormGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceExpertsExchange">
			<Columns>
				<asp:TemplateField>
					<ItemTemplate>
						<asp:Label runat="server" Text="<%# Container.DataItemIndex / 2 + 1 %>"></asp:Label>
					</ItemTemplate>
					<AlternatingItemTemplate>
						<asp:Label ID="Label1" runat="server" Text="No Index Every Other Row"></asp:Label>
					</AlternatingItemTemplate>
				</asp:TemplateField>
				<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
					SortExpression="Id" />
				<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
			</Columns>
		</asp:GridView>
		<asp:SqlDataSource ID="SqlDataSourceExpertsExchange" runat="server" ConnectionString="<%$ ConnectionStrings:ExpertsExchangeConnectionString %>"
			SelectCommand="SELECT [Id], [Name] FROM [GridTable]"></asp:SqlDataSource>
	</div>
	</form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveJellisonCommented:
Sorry, obviously replace the data connection with your own. I just had to wire something up to ensure the "row count" displayed how you described.
0
guru_samiCommented:
You can use alternatingTemplate like this:
<asp:TemplateField>
 
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<AlternatingItemTemplate>
           //show or don't show
</AlternatingItemTemplate>
</asp:TemplateField>

Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.alternatingitemtemplate.aspx?PHPSESSID=tn8k5...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.