Monday 20 October 2014

How to add Data Item Index in Grid View In Asp.Net

The GridView control is the successor to the DataGrid and extends it in a number of ways. While developing GridView control in ASP.NET, programmers often requires to display row number in GridView controls.
This can be accomplished by addingtthe Container.DataItemIndex  in the html markup of the Gridview control.


HTML
<asp:GridView ID="GridView1" runat="server" Width="100%" EmptyDataText="Ashokkkkiji" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
    <Columns>
        <asp:TemplateField HeaderText="Sno">
            <ItemTemplate>
            <span>
                <%#Container.DataItemIndex + 1 %>
            </span>
               </ItemTemplate>
        </asp:TemplateField>
    </Columns>
                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <SortedAscendingCellStyle BackColor="#EDF6F6" />
    <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
    <SortedDescendingCellStyle BackColor="#D6DFDF" />
    <SortedDescendingHeaderStyle BackColor="#002876" />
                    </asp:GridView>

C#      
private void bindgrid()
    {
       con.Open();
        SqlCommand cmd = new SqlCommand("Select New_Employee_Entry.Emp_Name,New_Employee_Entry.Account_No,New_Employee_Entry.Emp_PANCardNo,Salary_Table.Net_Payment FROM New_Employee_Entry INNER JOIN Salary_Table ON New_Employee_Entry.EmpID=Salary_Table.EmpID ORDER BY New_Employee_Entry.Emp_Name", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();      
        if (ds.Tables[0].Rows.Count > 0)
        {
           GridView1.DataSource = ds;
            GridView1.DataBind()
        }
        else
       {       
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());        
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }
    }






0 comments::

Post a Comment

Copyright © DotNet-Ashok