24h購物| | PChome| 登入
2011-11-17 16:42:40| 人氣4,903| 回應1 | 上一篇 | 下一篇

[技術]GridView新增 移除列

推薦 0 收藏 0 轉貼0 訂閱站台

資料來源:Shadow和愉快的程式碼伙伴

目標功能:

點選GridView的CommandField後,在該列下面新增一列並把剛剛新增列移除掉

.aspx

<%@ Page Debug="true" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource runat="server" id="sds_Categories"
            ConnectionString="<%$ ConnectionStrings:NorthwindChineseConnectionString %>"
          
            SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [Picture] FROM [Categories]"
         >
        </asp:SqlDataSource>
        <asp:GridView runat="server" ID="gvw_Categories" AutoGenerateColumns="False"
            DataKeyNames="CategoryID" DataSourceID="sds_Categories"
            onrowcommand="gvw_Categories_RowCommand" AllowPaging="True" PageSize="5" >
            <Columns>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                    InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
                    SortExpression="CategoryName" />
                <asp:BoundField DataField="Description" HeaderText="Description"
                    SortExpression="Description" />
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Open" Text="此列下面增一列"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
             
        </asp:GridView>
    </div>
    </form>
</body>
</html>

.cs

using System;
using System.Collections.Generic;
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 gvw_Categories_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Open")
        {
 
            int currentIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
 
            if (ViewState["addIndex"] != null )
            {
                
                    gvw_Categories.Controls[0].Controls.RemoveAt(Convert.ToInt32(ViewState["addIndex"]));
                    int pageIndex = gvw_Categories.PageIndex;//先把目前的pageIndex暫存起來
                    gvw_Categories.DataBind();
                    gvw_Categories.PageIndex = pageIndex;
                
            
            }
 
 
            GridViewRow gvr = new GridViewRow(-1, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell cell = new TableCell();
            cell.ColumnSpan = 3;
            cell.Text = "Hello World!!";
            gvr.Cells.Add(cell);
            gvw_Categories.Controls[0].Controls.AddAt(currentIndex + 2, gvr);
            ViewState["addIndex"] = currentIndex + 2;
 
 
 
        }
    }
 
    protected void gvw_Categories_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ViewState["addIndex"] = null; //ViewState["addIndex"]重設
        gvw_Categories.PageIndex = e.NewPageIndex;
        gvw_Categories.DataBind();
    }
}

執行效果:

預設畫面

001

點選第一列

002

點選第二列

003

 


台長: nut
人氣(4,903) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 技術 |
此分類下一篇:[ASP.NET] 利用 Gridview PreRender 計算統計、總數 -- 插入新行
此分類上一篇:[技術] 購買與安裝SSL憑證完全攻略

日本藤素
感謝分享!

http://www.yyj.tw/
2020-01-05 10:06:23
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文