//此例是在RowCommand 的事件中执行 可以另行在其他事件中执行
//页面内容
<asp:GridView ID="GridView2" AutoGenerateColumns="False" runat="server"
onrowcommand="GridView2_RowCommand">
<Columns>
<asp:BoundField DataField="ProStartDate" HeaderText="时间" />
<asp:BoundField DataField="employeeid" HeaderText="号码"/>
<asp:BoundField DataField="Truename" HeaderText="姓名" />
<asp:BoundField DataField="Handphone" HeaderText="手机" />
<asp:BoundField DataField="BankAccount" HeaderText="银行" />
<%-- <asp:TemplateField HeaderText="发送" SortExpression="IsPost">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("IsPost") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton CommandName="btnshow" ID="ImageButton1" CommandArgument='<%# Eval("OrderId") %>' runat="server" style="width: 14px" ImageUrl="~/images/新增.bmp" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
CommandName="btnshow" Text="按钮" CommandArgument='<%# Eval("OrderId") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Azure" Font-Size="10px" Width="10px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="Center" BackColor="#FFFFCC" BorderStyle="None" BorderWidth="0px" ForeColor="#330099" />
<PagerSettings Visible="False" />
</asp:GridView>
//后置代码中
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("btnshow"))
{
//创建一个GridView的一个分隔行(根据DataControlRowType来设置)
// GridViewRow rowSeparator = new GridViewRow(0, 0, DataControlRowType.Separator, DataControlRowState.Normal);
//或实现一个数据行
GridViewRow rowSeparator = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
//设置行的底色
rowSeparator.BackColor = System.Drawing.Color.White;
// 获取到行索引 RowIndex
GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = gvrow.RowIndex;
//获得当前的GridView
GridView gv = (GridView)sender;
//设置单元格集
TableCellCollection cells = gv.Rows[index].Cells;
//设置单元格,根据实际情况增加,我这儿是增加一个跨所有列的行
TableCell separatorCell = new TableCell();
//根据GridView的第一列的显示情况设置单元格和跨列数
if (GridView2.Columns[0].Visible == true)
{
//当前增加的行所跨的列是gv 的所有列
separatorCell.ColumnSpan = cells.Count;
}
else
{
separatorCell.ColumnSpan = cells.Count - 1;
}
//单元格的对齐
separatorCell.HorizontalAlign = HorizontalAlign.Center;
//单元格的背景色
separatorCell.BackColor = System.Drawing.Color.FromArgb(226, 226, 226);
//单元格的高度
separatorCell.ControlStyle.Height = 10;
//在列中添加一个控件
Label l = new Label();
l.Text = "ddddd";
Control c = (Control)l;
separatorCell.Controls.Add(c);
//在单元格集中增加单元格控件
rowSeparator.Cells.Add(separatorCell);
//设置GridView行的可见性
rowSeparator.Visible = true;
//在GridView中的相应行插入行
GridView2.Controls[0].Controls.AddAt(index + 2, rowSeparator);
//判断当前按钮点了几次了 同一个按钮点击第一次是添加下面的一行,点击第二次死隐藏当前添加的行
//ViewState["flat"] 用于标记当前点了几行
if (ViewState["flat"] != null)
{
if (ViewState["flat"].ToString().Trim().Equals(e.CommandArgument.ToString()))
{
rowSeparator.Visible = false;
ViewState["flat"] = null;
}
else ViewState["flat"] = e.CommandArgument.ToString();
}
else
ViewState["flat"] = e.CommandArgument.ToString();
}
// 注意: ViewState["flat}=null; 在pageLoad 事件中要先创建
}