asp.NetのGridViewノート


1、カスタムハイパーリンクフィールド
<asp:HyperLinkField Text="  ..."
            datatextformatstring="{0:c}"
            datanavigateurlfields="uid"
            datanavigateurlformatstring="~\details.aspx?uid={0}"         
            target="_blank"  />

または表示フィールドへのリンクのバインド
 
<asp:HyperLinkField DataTextField="username"
            datatextformatstring="{0:c}"
            datanavigateurlfields="uid"
            datanavigateurlformatstring="~\details.aspx?uid={0}"         
            target="_blank"  />

 
2、行の編集はまずウィザードで編集と削除ボタンを追加し、ボタンにイベントを追加する
 
//         
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
             dataBind();
        }

//     ,       
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
             dataBind();
        }

//     ,       
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //    DataKeyNames="uid"      
             string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
             //              0  
             string username = GridView1.Rows[e.RowIndex].Cells[1].Text;
            //      。。。。。。。。。。。。
            GridView1.EditIndex = -1;  
             dataBind();
        }

//       
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
             //    DataKeyNames="uid"      
             string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
             //              0  
             string username = GridView1.Rows[e.RowIndex].Cells[1].Text;
            //      。。。。。。。。。。
             dataBind();
        }
//          AllowPaging="True" PageSize="2"
         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
         {
             //      +1
             Response.Write(e.NewPageIndex.ToString());
         }

 
ページ参照コード
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True"
        DataKeyNames="uid" Width="392px" onrowediting="GridView1_RowEditing"
        AllowPaging="True" PageSize="2" onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowupdating="GridView1_RowUpdating"
        onrowdeleting="GridView1_RowDeleting" onpageindexchanging="GridView1_PageIndexChanging" >
        <Columns>
            <asp:BoundField DataField="uid" HeaderText="id " InsertVisible="False"
                ReadOnly="True" SortExpression="uid" />
            <asp:BoundField DataField="username" HeaderText="   "
                SortExpression="username" />
            <asp:BoundField DataField="sex" HeaderText="  " SortExpression="sex" />
            <asp:HyperLinkField DataTextField="username"
            datatextformatstring="{0:c}"
            datanavigateurlfields="uid"
            datanavigateurlformatstring="~\details.aspx?uid={0}"         
            target="_blank"  />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        
    </asp:GridView>