RealWorld.Grids(BulkEditGridView)多行编辑列表控件

翻译|其它|编辑:郝浩|2007-07-18 10:09:36.000|阅读 1401 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

        asp.net GridView 只能进行单行编辑(设置 EditIndex),相比之下,这个控件则可以进行多行同时编辑。同时还能进行多行插入。再配合 DataSource 控件,真的比较好用。

       1:配合 DataSource 控件使用

       2:多行编辑

       3:多行插入

       4:处理修改/插入事件

       5:分页

       6:模板的使用

       7:小扩展

       这是一个运行效果图。

 

这是下载地址:http://www.codeplex.com/ASPNetRealWorldContr/Release/ProjectReleases.aspx?ReleaseId=1674

配合使用Datasource控件。

        再提供的例子里面,数据源使用的是 SqlDataSource 控件。配置好 sql 语句就可以了。因为我的用例里面多数都是使用 ORM的,所以我用了另外一种 DataSource 控件。这个数据源控件的好处是对分页支持非常好,可以直接内置在对象的sql语句里面,真的可以做到数据库分页和 UI 分页结合。

        因为设计的关系,如果不使用 DataSource 控件,将很难使用到多行插入的功能。

    private void InsertRow(int rowIndex, bool causesValidation)

        {

            GridViewRow row = null;

            if ((!causesValidation || (this.Page == null)) || this.Page.IsValid)

            {

                DataSourceView dsv = null;

                bool useDataSource = base.IsBoundUsingDataSourceID;

                if (useDataSource)

                {

                    dsv = this.GetData();

                    if (dsv == null)

                    {

                        throw new HttpException("DataSource Returned Null View");

                    }

                 }

                 }

}

多行编辑

        其实这个控件把所有行都变成了编辑状态。这种用例可能在某些场合非常有用。

        BulkEditGridView 直接提供一个 DirtyRows 的属性,把修改行触发事件。

    protected void BulkEditGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        //user logic here.

        string s = e.NewValues["Manhour"].ToString().Trim();

        if (s == "0" || s == "")

            e.Cancel = true; //can do the cancel here.

        else

        {

            e.NewValues["Amount"] = 100;//test reset some value

        }

    }

        这样,就可以得到数据了。

        如果不使用 Datasource 控件,需要做些特别的处理

    if (EditableGrid.DirtyRows[EditableGrid.DirtyRows.Count-1].RowIndex == e.RowIndex)

            BindData();

        这样做的原因是:修改完成之后要重新绑定数据,而这个事件会被重复触发,所以一定要判断出来是不是最后一次触发这个事件,要不然直接绑定新数据的话会把其他行覆盖掉。

多行插入

        这个功能看起来比较有意思:一个 GridView 下边会有多个新行以供用户书写新的数据。用户可以设置现实多少个新行。

不过这个功能感觉多有不方便的地方。比方说:

  1. 很难得到新行的 GridViewRow 属性。解决方法是自己扩展 GridViewInsertEventArgs 类。这种调用失败:(EditableGrid.Rows[args.RowIndex].Cells[0].Controls[0] as TextBox).Text
  2. 无法得到新行创建的事件。在 OnRowCreted 事件里面只有普通行的,找不到新行的创建事件。他的实现里面是这样的:this.InnerTable.Rows.AddAt(index, row);

在提供的新的事件里面,我们可以处理插入:

    protected void BulkEditGridView1_RowInserting(object sender, RealWorld.Grids.GridViewInsertEventArgs args)

    {

        string s = args.NewValues["Manhour"].ToString().Trim();

        if (s == "0"||s=="")

            args.Cancel = true;

        else

        {

            //args.Row is my new GridViewInsertEventArgs property.

            DropDownList list = args.Row.FindControl("DropDownList1") as DropDownList;

            string x = list.SelectedValue;

        }

    }

注意:上边的 args.Row 是我自己扩展出来的。

这个事件也会循环触发。

分页

          我使用的是一个 DataSource 控件,所以处理分页正好使用。因为 BulkEditGridView 控件本身就继承自 GridView,所以分页,排序等功能继续使用没有问题。

模板列

          GridView 的模板列可以继续使用。但是在 InsertRow 的时候需要自己做一些扩展(原因就是上边说的无法得到当前事件 Row 的参数的问题)。

  1. 无法得到当前事件 Row 的参数的问题。这个可以通过扩展 GridViewInsertEventArgs 类来实现,上边已经说了。
  2. 无法得到新建 row 的创建事件的问题。这个也需要自己做一些扩展。主要是在 int index = this.InnerTable.Rows.Count - (this.ShowFooter ? 1 : 0);

            this.InnerTable.Rows.AddAt(index, row);附近,自行触发一个事件。

          这种情况可能出现在:设置了模板,但是无法控制 InsertRow 里面的某些非数据绑定控件(Dropdownlist或者其他)。

其他

DropDownField 类:这是提供的一个可以绑定列表的绑定列。类似的自己可以扩展其他绑定列。

附,扩展之后的 GridViewInsertEventArgs

GridViewInsertEventArgs

  public class GridViewInsertEventArgs : CancelEventArgs

     {

         private int _rowIndex;

         private IOrderedDictionary _values;

         private GridViewRow _row;

        public GridViewInsertEventArgs(int rowIndex)

             : base(false)

          {

            this._rowIndex = rowIndex;

        }

        /**//// <summary>

        /// new method, to expose the current row

        /// </summary>

        /// <param name="row"></param>

        public GridViewInsertEventArgs(int rowIndex,GridViewRow row)

            : base(false)

        {

            this._rowIndex = rowIndex;

            this._row = row;

        }

        /**//// <summary>

        /// Gets a dictionary containing the revised values of the non-key field name/value

        /// pairs in the row to update.

        /// </summary>

        public IOrderedDictionary NewValues

        {

            get

            {

                if (this._values == null)

                {

                    this._values = new OrderedDictionary();

                }

                return this._values;

            }

        }

        /**//// <summary>

        /// Gets a dictionary containing the revised values of the non-key field name/value

        /// pairs in the row to update.

        /// </summary>

        public GridViewRow Row

        {

           get

            {

                 return this._row;

            }

            set {

                this._row = value;

            }

        }

        /**//// <summary>

        /// Gets the index of the row being updated.

        /// </summary>

        public int RowIndex { get { return this._rowIndex; } }

    }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP