Friday, 11 May 2012

Example: How to implement the multi-edit functionality with a multi-page grid that is bound dynamically

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v10.2" Namespace="DevExpress.Web.ASPxHiddenField"
    TagPrefix="dxhf" %>
<%@ register Assembly="DevExpress.Web.ASPxGridView.v10.2" Namespace="DevExpress.Web.ASPxGridView"
    TagPrefix="dxwgv" %>
<%@ register Assembly="DevExpress.Web.ASPxEditors.v10.2" Namespace="DevExpress.Web.ASPxEditors"
    TagPrefix="dxe" %>

<!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 id="Head1" runat="server">
    <title></title>
      <script type="text/javascript">
        function ProcessValueChanged(key,animalName,colourID) 
        {
     
            var currentKey = "key" + key.toString();

            
            if (!clientHiddenField.Contains(currentKey)) 
            {

                clientHiddenField.Add(currentKey, animalName+";"+colourID);
            }
           
            
            else 
            {
                clientHiddenField.Set(currentKey, animalName + ";" + colourID);
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <dxwgv:aspxgridview ID="ASPxGridView1" runat="server"  KeyFieldName="ID" AutoGenerateColumns="False"
        SettingsBehavior-AllowDragDrop="False"  ClientInstanceName="clientGridView">
                <Columns>

                  <dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="0" Width="15px">
                  </dxwgv:GridViewDataTextColumn>

                  <dxwgv:GridViewDataTextColumn FieldName="Animal" VisibleIndex="1">

                    <DataItemTemplate> 
                       <dxe:ASPxTextBox ID="tbWbsLevel" runat="server"  Text='<%#Bind("Animal")%>' Size="6"
OnDataBound="tbWbsLevel_Load" >
                       </dxe:ASPxTextBox> 
                   </DataItemTemplate> 

                </dxwgv:GridViewDataTextColumn>

               <dxwgv:GridViewDataTextColumn FieldName="ColourID" Caption="Colour" VisibleIndex="2" >
                   <dataitemtemplate>
                     <dxe:aspxcombobox ID="colourBox" runat="server" Value='<%#Bind("ColourID") %>'
OnDataBound="colourBox_Load">
                       <items>
                         <dxe:listedititem Text="Red" Value="1" />
                         <dxe:listedititem Text="Gray" Value="2" />
                         <dxe:listedititem Text="Brown" Value="3" />
                         <dxe:listedititem Text="Black" Value="4" />
                       </items>
                     </dxe:aspxcombobox>
                   </dataitemtemplate>
               </dxwgv:GridViewDataTextColumn>
</Columns>

           <settingsbehavior AllowDragDrop="False" />
           <settingspager PageSize="5">
           </settingspager>

          </dxwgv:aspxgridview>
          
    <dxhf:aspxhiddenfield runat="server" ID="hiddenField" 
            ClientInstanceName="clientHiddenField">
    </dxhf:aspxhiddenfield>
        <br />
        <dxe:aspxbutton ID="btnUpdate" runat="server" Text="Update" 
            OnClick="btnUpdate_Click">
        </dxe:aspxbutton>
    </form>
</body>
</html>



C#........................................................................................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    private DataTable Data
    {
        get
        {
            DataTable data = (DataTable)Session["Data"];
            if (data == null)
            {
                data = new DataTable();
                data.Columns.Add("ID");
                data.Columns.Add("Animal");
                data.Columns.Add("ColourID");
                data.Rows.Add(new object[] { 1, "Fox", 1 });
                data.Rows.Add(new object[] { 2, "Wolf", 2 });
                data.Rows.Add(new object[] { 3, "Bear", 3 });
                data.Rows.Add(new object[] { 4, "Panther", 4 });
                data.Rows.Add(new object[] { 5, "Rat", 2 });
                data.Rows.Add(new object[] { 6, "Cat", 4 });
                Session["Data"] = data;
            }
            return data;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        ASPxGridView1.DataSource = Data;
        ASPxGridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Data.Rows.Count; i++)
        {
            string hfKey = "key" + Data.Rows[i]["ID"].ToString();
            if (hiddenField.Contains(hfKey))
            {
                string[] pars = Convert.ToString(hiddenField[hfKey]).Split(';');
                Data.Rows[i]["Animal"] = pars[0];
                Data.Rows[i]["ColourID"] = pars[1];


            }


        }
        ASPxGridView1.DataBind();
        hiddenField.Clear();
    }
    protected void tbWbsLevel_Load(object sender, EventArgs e)
    {
        GridViewDataItemTemplateContainer c = ((ASPxTextBox)sender).NamingContainer
            as GridViewDataItemTemplateContainer;
        ((ASPxTextBox)sender).ClientInstanceName = "animalName" + c.KeyValue.ToString();
        ((ASPxTextBox)sender).ClientSideEvents.TextChanged = "function(s,e){ProcessValueChanged(" +
c.KeyValue.ToString() + ",s.GetText(),animalColour" + c.KeyValue.ToString() + ".GetValue());}";

        string hfKey = "key" + c.KeyValue.ToString();
        if (hiddenField.Contains(hfKey))
        {
            string[] pars = Convert.ToString(hiddenField[hfKey]).Split(';');
            ((ASPxTextBox)sender).Text = pars[0];


        }

    }

    protected void colourBox_Load(object sender, EventArgs e)
    {
        GridViewDataItemTemplateContainer c = ((ASPxComboBox)sender).NamingContainer
         as GridViewDataItemTemplateContainer;
        ((ASPxComboBox)sender).ClientInstanceName = "animalColour" + c.KeyValue.ToString();
        ((ASPxComboBox)sender).ClientSideEvents.SelectedIndexChanged = "function(s,e){ProcessValueChanged(" +
c.KeyValue.ToString() + ",animalName" + c.KeyValue.ToString() + ".GetText(),s.GetValue().toString());}";
        string hfKey = "key" + c.KeyValue.ToString();
        if (hiddenField.Contains(hfKey))
        {
            string[] pars = Convert.ToString(hiddenField[hfKey]).Split(';');
            ((ASPxComboBox)sender).Value = pars[1];


        }
    }
}

No comments:

Post a Comment