Merhaba arkadaslar bu makalemizde GridView da veri gösterecegimiz satirlara otomatik olarak sira numarasi verecegiz. Önce GridView in AutoGenerateColumns="false" yapiniz. GridView in ItemTemplate içersine ;
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
Seklinde kod parçasiyla sira numarasini 1’er artirarak yazacagiz.
Screenshot
Sekil 1
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
if (!IsPostBack)
{
bindData();
}
}
}
protected void bindData()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Id No" ItemStyle-Font-Bold="true" ItemStyle-BackColor="LightYellow" ItemStyle-ForeColor="Green">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="yazar" HeaderText="Yazar" />
<asp:BoundField DataField="kitap" HeaderText="Kitap" />
<asp:BoundField DataField="fiyat" HeaderText="Fiyat" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüsmek üzere. Bahadir SAHIN
0 comments:
Yorum Gönder