
test1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label98" runat="server" Text=""></asp:Label><br>
<asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1" PageSize="10" OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender" EmptyDataText="沒有相應的記錄" DataKeyNames="CustomerID" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:CheckBox ID="ckbSelect" runat="server" Checked='<%#Eval("CustomerID").ToString()=="1"?true:false%>' Text=''></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="ID" ReadOnly="True" SortExpression="CustomerID">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CompanyName" HeaderText="公司名" ReadOnly="True" SortExpression="CompanyName">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ContactName" HeaderText="聯絡人" SortExpression="ContactName">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ContactTitle" HeaderText="職稱" SortExpression="ContactTitle">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="地址" SortExpression="Address">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Country" HeaderText="國別" SortExpression="Country">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource id="SqlDataSource1" runat="server" SelectCommand="" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>" >
</asp:SqlDataSource><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="取值" PostBack="True"/><br>
<asp:Label ID="Label99" runat="server" Text=""></asp:Label><br>
</form>
</body>
</html>
test1.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class test1 : System.Web.UI.Page
{
private void Page_InitComplete(object sender, System.EventArgs e)
{
this.SqlDataSource1.SelectCommand = "select CustomerID, CompanyName, ContactName, ContactTitle, Address, Country from Customers";
this.SqlDataSource1.DataBind();
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
//隱藏地址資料欄表頭
GridView1.HeaderRow.Cells[5].Visible = false;
//隱藏地址資料欄資料
for (Int32 i = 0; i <= GridView1.Rows.Count - 1; i++) {
GridView1.Rows[i].Cells[5].Visible = false;
}
foreach (GridViewRow wkItem in GridView1.Rows) //將第一行資料做rowspan=2
{
wkItem.Cells[0].RowSpan = 2;
}
}
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
SetMultiRow(e);
}
private void SetMultiRow(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
GridViewRow row;
row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell_1 = new TableCell();
cell_1.ColumnSpan = 1;
cell_1.Text = "地址---";
cell_1.BackColor = System.Drawing.Color.FromArgb(233,239,255);
row.Cells.Add(cell_1);
TableCell cell_2 = new TableCell();
cell_2.ColumnSpan = 4;
cell_2.Text = DataBinder.Eval(e.Row.DataItem, "Address").ToString().Replace("\r\n", "<br>");
cell_2.BackColor = System.Drawing.Color.FromArgb(255,206,128);
row.Cells.Add(cell_2);
e.Row.Parent.Controls.Add(row);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder strBuilder = new StringBuilder();
//foreach (GridViewRow row in GridView1.Rows)
//{
// strBuilder.AppendFormat("國別:{0},地址:{1}<br />", row.Cells[6].Text, row.Cells[5].Text);
//}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gvr = GridView1.Rows[i];
CheckBox ckb = (CheckBox)gvr.FindControl("ckbSelect");
if (ckb.Checked==true)
{
//Label99.Text += GridView1.Rows[i].Cells[6].Text + GridView1.Rows[i].Cells[5].Text + "<br>";
strBuilder.AppendFormat("國別:{0},地址:{1}<br />", GridView1.Rows[i].Cells[6].Text, GridView1.Rows[i].Cells[5].Text);
}
}
Label99.Text = strBuilder.ToString();
GridView1.DataBind(); //加入此DataBind後,下方顯示目前停留的頁次才會正確
}
}