我想要的效果或许你觉得没什么难的,但是,我确认这个问题真的不好解决!目标:
是做一个数据绑定到GridView的效果,但要完全具备以下效果:
1、可以编辑更新行;
2、每隔4行,插入一个分割行,在该分割行内显示一条"============"字符串;问题:
编辑更新行非常好做,这没有什么,但是,一旦我实现了第二目标(即:每隔4行插入一个分隔行)时,却意外的发现:
除了第一个分隔行之前的行,进行编辑没有问题,其他的行,在进行Edit后进行Update更新,均出现不触发执行RowUpdating事件,却意外的执行了下一行的RowEditing事件,所以非常困惑,研究了一周的时间,没有任何进展。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage08.aspx.cs" Inherits="TestPage_TestPage08" %><!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">
    <div>
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="true" SortExpression="ID" />
                <asp:TemplateField HeaderText="新闻标题" SortExpression="Title">
                    <ItemTemplate>
                        <asp:HyperLink ID="editTitle0" Text='<%# Eval("Title") %>' Target="_blank" runat="server"></asp:HyperLink>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="editTitle1" Text='<%# Eval("Title") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;using KingsLee_DataBaseManager_V40;//数据管理对象所在的命名空间,非常确认:这和我这个问题没有任何关系public partial class TestPage_TestPage08 : System.Web.UI.Page
{
    KLDBManager dbm;//数据库操作对象,不用管它,非常确认:这和我这个问题没有任何关系
    protected void Page_Load(object sender, EventArgs e)
    {
        dbm = new KLDBManager();        GridView1.DataKeyNames = new string[] { "ID" };        GridView1.AutoGenerateColumns = false;
        GridView1.AutoGenerateEditButton = true;        GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
        GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
        GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);        GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);        if (!IsPostBack)
        {
            ToBind();
        }
    }    void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        int _perRowCount = 4;
        int _columnSpan = 3;
        if (_perRowCount > 0 && e.Row.RowIndex >= 0)
        {
            if ((e.Row.RowIndex + 1) != 0 && (e.Row.RowIndex + 1) % _perRowCount == 0)
            {
                GridViewRow _row = new GridViewRow(0, e.Row.RowIndex, DataControlRowType.Separator, DataControlRowState.Normal);
                TableCell _cell = new TableCell();
                _cell.ColumnSpan = _columnSpan;
                _cell.Text = "=========================";
                _row.Cells.Add(_cell);
                GridView1.Controls[0].Controls.Add(_row);
                //GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + 1, _row);
            }
        }
    }    void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string _title = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("editTitle1")).Text.Trim();
        string _id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sqlEdit = "UPDATE TDL_Article SET Title='" + _title + "' WHERE ID=" + _id;
        dbm.ToDo(sqlEdit, false, false);
        GridView1.EditIndex = -1;
        ToBind();
    }    void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        ToBind();
    }    void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        ToBind();
    }    void ToBind()
    {
        string sqlString1 = "SELECT TOP 20 ID,Title FROM TDL_Article";
        DataSet ds = dbm.GetDs(sqlString1);//获得DataSet,不用管它,非常确认:这和我这个问题没有任何关系
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

解决方案 »

  1.   

    很明显,你破坏了Rows的排序。原来的第4行,现在跑到地5行去了,不可能变得正常。如果喜欢破坏控件次序,就自己写一个GridView控件。
      

  2.   

    我记得以前我遇到一个公司的asp.net开发员喜欢搞一个妖术,喜欢把页面上的一些控件Move到其它控件内部某个地方去。后来,这个开发人员头疼的撞墙,最重要地是他的项目只能流产了。除非你知道人家的控件树结构都是明确用ID来标记,而不是用次序来标记,否则你从中间插入任何东西都是在搞破坏。
      

  3.   

    实际上这有很多解决方法。在数据源中加入额外的行,或者为某一行加入style来实现它的bottom样式,或者在GridView的行中最后一列加入html代码,或者最直接的做法是使用更好的控件ListView,等等。
      

  4.   

    那有没有可以达到我的说的这两个目标的办法呢?即:
    1、可以编辑更新行;
    2、每隔4行,插入一个分割行,在该分割行内显示一条"============"字符串;PS:其实,我也知道破坏了RowIndex的正常绑定次序,只是为了实现目标才这么做的,所以莫怪哈~另外,我意外的发现:以我这样的程序操作时,
    点击"编辑"和"取消"时,触发的Edit事件的过程,RowIndex并没有任何错误;
    只有"更新"时,不能触发任何Update事件;测试结果:操作(第几行) RowIndex CommandName编辑(3): 2 Edit
    更新(3): 2 Update编辑(5): 3 Edit
    更新(5): 4 Delete编辑(10): 7 Edit
    更新(10): 9 Delete编辑(15): 11 Edit
    更新(15): 14 Delete
      

  5.   

    谢谢 sp1234 的回复~这段时间一直为这个问题研究着,我一直试图在以下几个方向找到突破口:
    1、通过控制甚至改变绑定时的RowIndex,或许可以解决Edit按钮和Update按钮的错位问题,因为我发现Edit的时候,RowIndex是没有错的,而Update事件触发的依据从测试上来看,它似乎和当前所处的行号有关系(而不是EditRowIndex);
    2、通过在行Create的时候来解决
      

  6.   

    在datalsit中可实现
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((e.Item.ItemIndex + 1) % 4 == 0)
            {
                Literal lit = new Literal();
                lit.Text = "";
                e.Item.Controls.Add(lit);
            }    }
      

  7.   


    “或者在GridView的行中最后一列加入html代码”能否把这个方法稍微详细点讲讲呢?谢谢!