Showing posts with label displaying gridview row index number. Show all posts
Showing posts with label displaying gridview row index number. Show all posts

Wednesday, November 9, 2011

Displaying ASPxGridView row index number

You can achieve this through two way:
Method 1
Step 1. Add ASPxGridView on your page:

<dx:ASPxGridView ID="grvTest" AutoGenerateColumns="False" runat="server"
DataSourceID="SqlDataSource1" onhtmlrowprepared="grvTest_HtmlRowPrepared"
onhtmlrowcreated="grvTest_HtmlRowCreated">
<Columns>
<dx:GridViewDataTextColumn Caption="RowID" Name="colRowID" VisibleIndex="0"
Width="20px">
<DataItemTemplate>
<dx:ASPxLabel ID="lblRowID" runat="server" Text="lablel">
</dx:ASPxLabel>
</DataItemTemplate>
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="User Name" FieldName="UserName"
Name="colUserName" VisibleIndex="1" Width="100px">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Password" FieldName="Password"
Name="colPassword" VisibleIndex="2" Width="30px">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Role" FieldName="Role" Name="colRole"
VisibleIndex="3" Width="30px">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>

Step 2: Add following ASPxGridView Event code snippet to your code – behind file (.cs)

protected void grvTest_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
{
if (e.RowType != GridViewRowType.Data) return;

ASPxLabel label = grvTest.FindRowCellTemplateControl(e.VisibleIndex, null, "lblRowID") as ASPxLabel;
label.Text = (e.VisibleIndex + 1).ToString();
}

Method 2
The another way to do it is this one in the Columns section of your GridView control:

<dx:ASPxGridView ID="grvTest" AutoGenerateColumns="False" runat="server"
DataSourceID="SqlDataSource1" >
<Columns>
<dx:GridViewDataTextColumn Caption="RowID" Name="colRowID" VisibleIndex="0"
Width="20px">
<DataItemTemplate>
<dx:ASPxLabel ID="lblRowID" runat="server" Text='<%# Container.ItemIndex + 1 %>'>
</dx:ASPxLabel>
</DataItemTemplate>
</dx:GridViewDataTextColumn>

Displaying ASP GridView row index number

You can achieve this through two way:
Method 1
Step 1. Add this your GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated" >

<Columns>
<asp:TemplateField HeaderText="RowId">
<ItemTemplate><asp:Label runat="server" />                      
</ItemTemplate>            
</asp:TemplateField> 
...
</Columns>


Step 2: Add following GridView Event code snippet to your code – behind file (.cs)


protected void GridView1_RowCreated(object sender, 
System.Web.UI.WebControls.GridViewRowEventArgs e)
{ 
if (e.Row.RowType == DataControlRowType.DataRow) {
// Retrieve the Label from the first column.
Label myLabel = (Label)e.Row.Cells[0].Controls[0]; 

// Set Label text equal to the rowIndex +1
myLabel.Text = e.Row.RowIndex.ToString() + 1; 
}
}


Method 2
The another way to do it is this one in the Columns section of your GridView control:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" PageSize="5">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>