0

我正在使用ASPxGridView和更新来自 DataTable 的数据,它工作正常。但是当我尝试使用编辑、更新、删除、分页、排序功能时,我遇到了麻烦。显示按钮,但当我单击它们时该功能不起作用。

这是我的Sample.aspx文件:

[ASPx]

<dx:aspxgridview ID="grid" ClientInstanceName="grid" runat="server" 
        OnDataBinding="grid_DataBinding" Width="100%" EnableCallBacks="false">        
    
    <Settings ShowTitlePanel="True" />
    <SettingsText Title="Tabla" />
    <SettingsPager PageSize ="10" ShowSeparators="true" PageSizeItemSettings-Items="20" >
        <PageSizeItemSettings Visible="true" />            
    </SettingsPager>
    
</dx:aspxgridview>

Sample.aspx.cs 背后的代码:

[C#]

using System;
using System.Data;
using System.Web.Mvc;
using DataConnector;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

namespace Sample.Views.Actions

{

    public partial class Sample: ViewPage
    {

        private DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                grid.DataBind();

            grid.RowValidating += new ASPxDataValidationEventHandler(grid_RowValidating);

            if (!this.IsPostBack)
            {
                GridViewCommandColumn c = new GridViewCommandColumn();
                grid.Columns.Add(c);                
                c.Caption = "Operations";

                c.EditButton.Visible = true;
                c.UpdateButton.Visible = true;
                c.NewButton.Visible = true;
                c.DeleteButton.Visible = true;
                c.CancelButton.Visible = true;
            }            
        }        

        DataTable BindGrid()
        {
            DataConnection DM = new DataConnection();
            if (DM.login("ADMIN", "PASS"))
            
                dt = DM.getDataSet("SELECT * FROM Table_Name");
                grid.DataSource = dt;
                dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };       
            
            return dt;
        }

        protected void grid_DataBinding(object sender, EventArgs e)
        {
            // Assign the data source in grid_DataBinding
            BindGrid();
        }

        protected void grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            int id = (int)e.OldValues["id"];
            DataRow dr = dt.Rows.Find(id);
            dr[0] = e.NewValues[""];
            dr[1] = e.NewValues[""];
            dr[2] = e.NewValues[""];
            dr[3] = e.NewValues[""];
            dr[4] = e.NewValues[""];

            ASPxGridView g = sender as ASPxGridView;
            UpdateData(g);
            g.CancelEdit();
            e.Cancel = true;
        }


        void grid_RowValidating(object sender, ASPxDataValidationEventArgs e)
        {
            int id = (int)e.NewValues["id"];
            if ((!e.OldValues.Contains("id") || ((int)e.OldValues["id"] != id))
                && (dt.Rows.Find(id) != null))
            {
                ASPxGridView grid = sender as ASPxGridView;
                e.Errors[grid.Columns["id"]] = String.Format("Column 'Id' is constrained to be unique.  Value '{0}' is already present.", id);
            }
        }


        private void UpdateData(ASPxGridView g)
        {
            g.DataBind();
        }
    }}

在控制器页面上调用示例:

[C#]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DataConnector;

namespace Sample.Controllers

{    
public class ActionsController : Controller

    {
        
        public ActionResult Sample()
        {
            return View();
        }

    }
}

网格上显示的数据。

我创建了一个新的解决方案,其中只有Sample.aspxandSample.aspx.cs并且我在没有 MVC 的情况下使用并且功能正在运行。不同之处在于“公共部分类示例:System.Web.UI.Page”。如果我尝试不System.Web.UI.Page使用我提到的代码,它会给我以下错误:

The view must derive from ViewPage, ViewPage<TModel>, ViewuserControl,or ViewuserControl<TModel>.

我究竟做错了什么?为什么 ASPxGridView 的功能不起作用?

如果起草得不好,但我的母语不是英语,我很抱歉,我是一名新程序员:)

4

1 回答 1

1

ASPxGridView 是一个 WebForms 控件,不能在 ASP.NET MVC 中使用。相反,您应该使用DevExpress MVC 扩展。具体来说,DevExpress MVC GridView 扩展

观看我的“DevExpress MVC 入门视频”以了解更多信息。

于 2012-11-26T18:25:32.473 回答