V.B
V.B

Reputation: 65

ASPxGridView not works in my ASP.NET MVC solution

I'm using ASPxGridView and updating with data from DataTable which, works fine. But when I'm trying to use edit, update, delete, paging, sorting functions I get in trouble. The buttons are displayed but the function doesn't work when I click them.

Here is my Sample.aspx file:

[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>

The code behind 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();
        }
    }}

The Sample is called on the controller page:

[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();
        }

    }
}

The data displayed on the Grid.

I created a new solution where is only the Sample.aspx and Sample.aspx.cs and I'm using without MVC and the functions are working. The difference is "public partial class Sample: System.Web.UI.Page ". If I try without System.Web.UI.Page on my mentioned code it gives me the following error:

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

What am I doing wrong? Why does not the function of the ASPxGridView working?

I'm sorry in advance if it is poorly drafted but not my mother tongue is English, and I am a new programmer :)

Upvotes: 0

Views: 1165

Answers (1)

Mehul
Mehul

Reputation: 848

The ASPxGridView is a WebForms control and cannot be used within ASP.NET MVC. Instead you should use the DevExpress MVC extensions. Specifically, the DevExpress MVC GridView extension

Watch my "Getting started with DevExpress MVC video" to learn more.

Upvotes: 1

Related Questions