一直都只有找到VB版 , 能力有限 , 修改不了
找這這篇C#版,好實用忍不住要把他記下來 ... 原文出處
步驟1.創造兩個GridView+SqlDataSource
步驟2.GridView1 的 RowDataBound 事件撰寫下面
1 |
<span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> GridView1_RowDataBound(<span style="color: #0000ff">object</span> sender, GridViewRowEventArgs e) |
1 |
{ |
1 |
<span style="color: #0000ff">if</span> ((e.Row.RowState & DataControlRowState.Selected) == DataControlRowState.Selected) |
1 |
{ |
1 |
GridViewRow oRow = <span style="color: #0000ff">new</span> GridViewRow(0, -1, DataControlRowType.DataRow, e.Row.RowState); |
1 |
GridView2.Visible = <span style="color: #0000ff">true</span>; |
1 |
GridView2.DataBind(); |
1 |
<span style="color: #0000ff">string</span> sHTML = ControlToHTML(GridView2); |
1 |
GridView2.Visible = <span style="color: #0000ff">false</span>; |
1 |
TableCell oCell = <span style="color: #0000ff">new</span> TableCell(); |
1 |
oCell.Text = sHTML; |
1 |
oCell.ColumnSpan = e.Row.Cells.Count; |
1 |
oRow.Cells.Add(oCell); |
1 |
e.Row.Parent.Controls.AddAt(e.Row.RowIndex + 2, oRow); |
1 |
} |
1 |
} |
步驟3加入兩段小小程式碼 不然不會動
1 |
<span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> ControlToHTML(System.Web.UI.Control Control) |
1 |
{ |
1 |
<span style="color: #0000ff">string</span> sHTML = <span style="color: #0000ff">string</span>.Empty; |
1 |
StringWriter oTextWriter = <span style="color: #0000ff">new</span> StringWriter(); |
1 |
HtmlTextWriter oHtmlWriter = <span style="color: #0000ff">new</span> HtmlTextWriter(oTextWriter); |
1 |
1 |
Control.RenderControl(oHtmlWriter); |
1 |
sHTML = Convert.ToString(oTextWriter); |
1 |
<span style="color: #0000ff">return</span> sHTML; |
1 |
} |
1 |
1 |
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> VerifyRenderingInServerForm(Control control) |
1 |
{} |
步驟4.在原始檔部分設定一下屬性
設定 GridView1的 EnableViewState="False"
設定 GridView2的 EnableViewState="False",且設定 Visible="False"
Page 的 EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" EnableEventValidation="false" %>
(Visited 793 times, 1 visits today)