かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[.NET][C#][ASP.NET]GridViewに連番をつける

GridViewの一番左端の列に連番をふりたくなった。
ということでれっつトライ。

いつもどおりの、Personクラスを用意する。今回は、ObjectDataSourceを使うつもりなので、データ取得のためのメソッドも定義した。

namespace WebApplication1.Data
{
    public class Person
    {
        public string Name { get; set; }

        /// <summary>
        /// テスト用データ
        /// </summary>
        /// <returns></returns>
        public static Person[] AllPeople()
        {
            return new[]
            {
                new Person { Name = "田中 太郎" },
                new Person { Name = "田中 二郎" },
                new Person { Name = "田中 三郎" },
                new Person { Name = "田中 四郎" },
                new Person { Name = "田中 五郎" },
                new Person { Name = "田中 六郎" },
                new Person { Name = "田中 七郎" },
                new Person { Name = "田中 八郎" },
                new Person { Name = "田中 九郎" },
                new Person { Name = "田中 十郎" },
            };
        }
    }
}

これが出来たら、画面にObjectDataSourceをPeopleDataSourceという名前で置いてPerson.AllPeopleを使うように設定する。

<asp:ObjectDataSource ID="PersonDataSource" runat="server" 
    SelectMethod="AllPeople" TypeName="WebApplication1.Data.Person"></asp:ObjectDataSource>

もちろん、この設定自体はデザイナからしている。(手書きで出来るほど覚えてない…)

そしたら、次にGridViewを置く。GridViewのDataSourceにPeopleDataSourceを設定してNameプロパティの値を表示するようにカラムを追加する。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="PersonDataSource">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    </Columns>
</asp:GridView>

これで、太郎~十郎までが表形式で表示されるようになる。
これに連番をふってみようと思う。連番をふるためにはTemplateFieldを定義しないといけない。
この中で、Containerというものが使えるので、その中にある

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="PersonDataSource">
    <Columns>
        <asp:TemplateField HeaderText="No">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    </Columns>
</asp:GridView>

これで連番が表示される!会社からBlog書いてるので画像UPできないのでどんな表が表示されたのかを見てもらうために、実行結果のHTMLを下に貼り付けてみる。

No Name
1 田中 太郎
2 田中 二郎
3 田中 三郎
4 田中 四郎
5 田中 五郎
6 田中 六郎
7 田中 七郎
8 田中 八郎
9 田中 九郎
10 田中 十郎

いい感じ。

投稿日時 : 2008年10月31日 12:59

Feedback

# モンクレール アウトレット通販 2012/11/08 2:42 http://moncler.katsu-ie.com/

こんにちは、またブログ覗かせていただきました。また、遊びに来ま~す。よろしくお願いします

# moncler jackets for men 2012/12/07 22:50 http://2012monclerdownjacket1.webs.com/

certainly like your website however you have to check the spelling on several of your posts. Several of them are rife with spelling problems and I to find it very bothersome to tell the reality nevertheless I will definitely come back again.

# sac longchamp soldes 2012/12/14 20:42 http://www.saclongchampachete.info/category/longch

I guess I'm not the only one having every one of the enjoyment listed here!

# burberry scarf sale 2012/12/15 22:58 http://www.burberryuksale.org/category/burberry-sc

I actually like em. It's different you utilize these out walking on town individuals.

# longchamp le pliage 2012/12/16 17:55 http://www.saclongchampachete.info/category/longch

That's exactly what earbuds will be for.

# www.burberrycanadaoutlet.info 2012/12/17 2:51 http://www.burberrycanadaoutlet.info

Thus, our shelves land up filled with stuffs that we appreciate.

# sacs guess soldes 2012/12/18 2:08 http://sacsguess.monwebeden.fr

Very useful info. Hope to find out more content articles soon!

# http://michael-kors-canada.webnode.fr/news-/ 2012/12/18 20:27 http://michael-kors-canada.webnode.fr/

this can be something we have never ever in your life read.

# women bags burberry 2012/12/18 20:57 http://www.burberryoutlet2012.info/category/burber

this might be something may possibly never ever previously read.

# chile 62 2013/01/08 2:57 http://www.robenuk.eu/

Please don't communicate ones joy and happiness to one lower happy in comparison with one self.
chile 62 http://www.robenuk.eu/

# chine destock 2013/01/08 7:09 http://www.destockchinefr.fr/nike-air-max-pas-cher

Don't communicate your primary glee to much less fortuitous compared with your body.
chine destock http://www.destockchinefr.fr/nike-air-max-pas-cher/nike-air-max-tn-pas-cher/

# casquette obey 2013/03/02 18:08 http://www.b66.fr/

Around the world may possibly one person, though to at least one woman may possibly the planet. casquette obey http://www.b66.fr/

# destockjeans 2013/03/02 18:08 http://www.g77.fr/

If you should probably would take care of your magic formula via an enemy, enlighten it by no means a. destockjeans http://www.g77.fr/

# Jordan Retro 11 2013/03/04 23:14 http://www.nikerow.com/

Even if one doesn‘longer accept you a task want them towards,doesn‘longer result in they will preceptor‘longer accept you boost they may of. Jordan Retro 11 http://www.nikerow.com/

# Air Jordan Retro 4 2013/03/06 20:54 http://www.jordanretro4air.com/

I prefer take a look at on account of whom you are, but nevertheless , on account of who exactly What i am next time i morning on you. Air Jordan Retro 4 http://www.jordanretro4air.com/

# casquette obey 2013/03/16 9:56 http://www.a44.fr/

If your goal is a fantastic data processing from the importance, rely your buddies. casquette obey http://www.a44.fr/

# destockchine 2013/03/24 0:35 http://d77.fr/

Need not it's the perfect time might be cozy to be with. To understand which will power one jimmy your self it. destockchine http://d77.fr/

# destockchine 2013/03/24 0:35 http://c99.fr/

In case you could preserve your formula from an opponent, know the application not to partner. destockchine http://c99.fr/

# usine23 2013/03/25 5:05 http://e55.fr/

The place where there may be romantic relationship with out really like, it will likely be really like with out romantic relationship. usine23 http://e55.fr/

# casquette dc 2013/04/03 22:17 http://www.laredoutesoldes.fr/

Usually do not communicate your primary peace to just one a lot happy in comparison to your spouse. casquette dc http://www.laredoutesoldes.fr/

# marquefr.fr 2013/04/07 17:57 http://www.marquefr.fr/

Simply because any individual doesn‘w not accept you how to desire them that will,doesn‘w not indicate that they'll father‘w not accept you effortlessly they want. marquefr.fr http://www.marquefr.fr/

# tati 2013/04/08 0:30 http://ruenee.com/

An accurate colleague will be just who overlooks all your outages and additionally can handle all your success. tati http://ruenee.com/

# DlHZtCnDrlCzMnc 2015/05/03 16:48 horny

pvFmAH http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# ivermectin 3mg tablets price 2021/09/28 16:55 MarvinLic

stromectol generic http://stromectolfive.com/# ivermectin canada

# buy prednisone from canada https://prednisonesnw.com/#
how can i order prednisone 2021/11/13 9:41 Prednisone

buy prednisone from canada https://prednisonesnw.com/#
how can i order prednisone

# cheap generic pills 2021/12/05 1:43 JamesDat

https://genericpillson.com/# generic pills prednisone

# bimatoprost 2021/12/12 3:29 Travislyday

http://baricitinibrx.com/ baricitinib

# bimatoprost buy online usa 2021/12/12 22:55 Travislyday

https://plaquenils.com/ hydroxychloroquine 800 mg

# bimatoprost ophthalmic solution careprost 2021/12/13 18:38 Travislyday

https://bimatoprostrx.com/ buy bimatoprost

# careprost bimatoprost ophthalmic best price 2021/12/14 14:23 Travislyday

http://baricitinibrx.online/ baricitinib coronavirus

# stromectol usa 2021/12/18 21:06 Eliastib

dhzmsv https://stromectolr.com stromectol covid

# generic ivermectin cream 2022/02/17 23:23 FrankAwabe

http://stromectolst.com/# ivermectin lotion
stromectol 3 mg price

# ivermectin 5 mg price 2022/02/18 11:10 FrankAwabe

http://stromectolst.com/# stromectol 3 mg tablet
ivermectin oral 0 8

# stromectol nz 2022/02/20 17:55 EmanuelMeaks

https://stromectolis.com/# stromectol price in india

# ivermectin 1 cream 45gm 2022/02/21 6:49 EmanuelMeaks

https://stromectolis.com/# ivermectin 5

# ivermectin usa 2022/02/22 0:54 EmanuelMeaks

https://stromectolis.com/# how much does ivermectin cost

# buy doxycycline online 270 tabs https://doxycyline1st.com/
doxycycline hydrochloride 100mg 2022/02/26 8:25 Jusidkid

buy doxycycline online 270 tabs https://doxycyline1st.com/
doxycycline hydrochloride 100mg

# Rew 5 Uvhs Dnt 2022/11/01 16:36 EesMEOJ

https://prednisoneall.top/

# Some trends of medicament. QAJP 2023/01/12 17:53 AxtIBIG

Самогон - это традиционный алкогольный напиток, производимый с помощью спиртовки (единственный вид спиртовки, который вы когда-либо найдете), и обычно изготавливаемый путем брожения ароматизаторов с сахаром и другими ингредиентами.
https://1samogon.ru/category/vino/

# koqxfrh 2023/02/11 8:16 arszraq

https://interlinkinfo.com/prednisone-20-mg/

# Cautions. Read now.
https://edonlinefast.com
Everything information about medication. Read here. 2023/02/17 11:51 EdPills

Cautions. Read now.
https://edonlinefast.com
Everything information about medication. Read here.

# Nyg 8 Bpub Xmt 2023/03/03 9:39 wwzjdly

https://indacrypt.com/

# Kzj 5 Jiyq Ffp 2023/03/03 14:00 wqedhza

Многие автовладельцы сталкивались с ситуацией утраты номерного знака. В теории каждый прекрасно знает, что в этом случае нужны дубликаты номерных знаков и требуется обращаться в организацию, которой выполняется официальное изготовление номеров на автомобиль.
https://guard-car.ru/

# djrjzzo 2023/03/15 14:38 aynxwix

https://multivarki-recepti.ru/chto-delat-esli-otkazyvayut-v-kreditah.html

# dzznlwb 2023/03/18 3:05 qjfnxyb

http://specodezh.ru/

# Vеry descriptive article, I loved that bit. Will there be a part 2? 2023/03/26 16:38 Very descriptive aгticle, I lovеd that bit. Will t

Very descriptive article, I loved that bit. Wi?l there be a part 2?

# Vеry descriptive article, I loved that bit. Will there be a part 2? 2023/03/26 16:39 Very descriptive aгticle, I lovеd that bit. Will t

Very descriptive article, I loved that bit. Wi?l there be a part 2?

# Vеry descriptive article, I loved that bit. Will there be a part 2? 2023/03/26 16:39 Very descriptive aгticle, I lovеd that bit. Will t

Very descriptive article, I loved that bit. Wi?l there be a part 2?

# Vеry descriptive article, I loved that bit. Will there be a part 2? 2023/03/26 16:40 Very descriptive aгticle, I lovеd that bit. Will t

Very descriptive article, I loved that bit. Wi?l there be a part 2?

# Yes! Finally someone writes about abilify. 2023/03/28 8:07 Yeѕ! Finally someone writes about abilify.

Υes! Finally someone ?rites about aЬilify.

# twydbhs 2023/04/07 1:45 wqbizd

https://v-mig.ru/recepty-prazdnichnogo-stola/

# kiatjmd 2023/04/07 3:59 yzznmh

https://8tracks.com/karnizyishtory

# gel per erezione in farmacia https://viasenzaricetta.com/# 2023/04/16 13:04 ViaSenza

gel per erezione in farmacia https://viasenzaricetta.com/#

# wqtddqb 2023/04/28 4:57 moepny

science https://www.gsu.by

# cytotec buy online usa - https://cytotecsale.pro/# 2023/04/29 4:35 Cytotec

cytotec buy online usa - https://cytotecsale.pro/#

# zbrixpt 2023/04/29 4:40 uxyugj

https://nexalive.com/nigerias-securities-regulator-establishes-fintech-unit-to-study-crypto/#comment-3627543

# Hll 7 Rduq Aeg 2023/05/03 21:01 bvjgdbh

https://englishpromo.ru/2023/04/samye-vazhnye-kriterii-kak-snyat-kvartiru-na-sutki/

# hkdhvzh 2023/05/12 5:44 qhvniv

https://vk.com/monolitnye_raboty_minsk?w=wall-213701595_36

# News Today 24 2023/05/16 3:51 Dannydaf

News Today 24 https://greennewdealnews.com/2021/02/21/the-worldwide-shift-from-oil-and-gas-to-green-energy/feed/

# News today 24 2023/05/16 17:50 Kevinpreop

News today 24 https://truehue.world/2020/03/09/how-woke-illiberalism-is-killing-the-academy/

# https://multfilmion.ru/ 2023/05/18 3:26 EdwardNeert

https://multfilmion.ru/

# https://multfilmtut.ru/ 2023/05/18 19:14 Waynejat

https://multfilmtut.ru/ Мультфильм тут

# ::::::: a.k.a films ::::::: http://akafilms.com/ 2023/05/24 4:36 AndrewErona

::::::: a.k.a films ::::::: http://akafilms.com/

# Multfilm http://computac.com/mobile/ 2023/05/26 0:05 AndrewErona

Multfilm http://computac.com/mobile/

# where to buy prednisone without prescription https://prednisonepills.pro/# - prednisone 10 mg tablets 2023/06/05 1:37 Prednisone

where to buy prednisone without prescription https://prednisonepills.pro/# - prednisone 10 mg tablets

# Услуга трезвый водитель 2023/06/15 2:04 Willienoize

Трезвый водитель Москва ? https://sober-driver77.business.site/ круглосуточный вызов водителя по Москве и Московской области, низкие цены, опытные водители.

# best otc ed pills https://edpill.pro/# - best pill for ed 2023/06/27 14:33 EdPills

best otc ed pills https://edpill.pro/# - best pill for ed

# ed pills for sale https://edpills.ink/# - ed treatment pills 2023/07/27 0:46 EdPills

ed pills for sale https://edpills.ink/# - ed treatment pills

# аккумуляторы 2023/08/10 17:10 Keithinted

Наши аккумуляторы обеспечивают высокую производительность и надежность, что является ключевым фактором для бесперебойной работы вашего автомобиля в любых условиях https://digicar.com.ua/

# generic plavix https://plavix.guru/ cheap plavix antiplatelet drug 2023/10/24 0:39 Plavixxx

generic plavix https://plavix.guru/ cheap plavix antiplatelet drug

# how much is valtrex in canada https://valtrex.auction/ valtrex brand name price 2023/10/24 22:04 Valtrex

how much is valtrex in canada https://valtrex.auction/ valtrex brand name price

# pills erectile dysfunction https://edpills.tech/# pills for ed 2023/12/23 8:07 EdPills

pills erectile dysfunction https://edpills.tech/# pills for ed

# lana rhoades unleashed - https://lanarhoades.pro/ lana rhoades videos
2024/03/06 17:50 LanaRho

lana rhoades unleashed - https://lanarhoades.pro/ lana rhoades videos

# eva elfie full video https://evaelfie.site/ eva elfie hd
2024/03/07 6:20 EvaElfie

eva elfie full video https://evaelfie.site/ eva elfie hd

# como jogar aviator https://aviatormocambique.site como jogar aviator
2024/03/12 1:06 AviatorMaz

como jogar aviator https://aviatormocambique.site como jogar aviator

# aviator: https://aviatorghana.pro/ aviator sportybet ghana
2024/03/12 10:54 AviatorGh

aviator: https://aviatorghana.pro/ aviator sportybet ghana

# pin up g&#252;ncel giri&#351;: https://pinupgiris.fun/ pin-up bonanza
2024/03/28 9:07 GirisUp

pin up g&#252;ncel giri&#351;: https://pinupgiris.fun/ pin-up bonanza

タイトル
名前
Url
コメント