Forums
Forums are Read-Only. Use the new Support Center. To start a general discussion, use the General category when submitting your question.

Using modal popup edit form for updating multiple rows

Last post 2/4/2010 10:43 AM by Andy Tr (DevExpress). 4 replies.
Sort Posts: Previous Next
  • Chris Rutledge

    Using modal popup edit form for updating multiple rows

    5/12/2008 3:42 PM
    • Not Ranked
    • Joined on 12/10/2007
    • Posts 26

    Hi,

    I'd like to implement multi-edit functionality in my grid similar to the way it's done in iTunes.  A user selects multiple rows and clicks an 'edit' button and the modal pop-up appears.  Whatever values the user inserts into the fields gets applied to all the selected rows.

    I thought I could do this easily by setting the edit mode to PopupEditForm and executing grid.StartEdit(), but that method required the index of the item about to be edited so it can pre-populate the edit form.  Since I'm going to have several rows selected I don't want it to be pre-populated at all.

    I could probably just feed it an index, then capture one of the edit cell generation events and blank it out, but I'm wondering if there's a method you folks suggest I use.

    Also, I would really like the edit form to come up with grayed-out cells at first.  If the user clicks inside the cell it is marked 'changed' or 'dirty', and that value (which can include an empty string) would be applied to all the selected rows.  Does the devex grid's edit form have that kind of logic?  I know some of the windows forms cells do.

    Thanks for any guidance you can give me,

    Chris

     

  • Nathan (DevExpress)

    Re: Using modal popup edit form for updating multiple rows

    5/14/2008 2:59 AM
    • Top 25 Contributor
    • Joined on 4/23/2007
    • Posts 1,310

     Hi Chris,

    You can do the following:

    protected void ASPxButton1_Click(object sender, EventArgs e) {

        if (ASPxGridView1.Selection.Count == 0) return;

        ASPxGridView1.StartEdit(ASPxGridView1.FindVisibleIndexByKeyValue(ASPxGridView1.GetSelectedFieldValues("ProductID")[0]));

    }

    protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {

        if (e.Column.ReadOnly) return;

        e.Editor.Value = string.Empty;

    }

    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {

        List<object> selectedRowKeys = ASPxGridView1.GetSelectedFieldValues("ProductID");

        // e.NewValues - contains the values of the non-key field name/value pairs in the row to be updated.

        // The primary key field isn't included in this dictionary.

        SaveDataToDatabase(selectedRowKeys, e.NewValues); // You should manually implement this function.

        e.Cancel = true;

    }

     

    Thanks, Nathan.
    R&D, .NET Team, Developer Express Inc.
    P.S. If you wish to receive direct assistance from our Support Team, use Support Center at http://www.devexpress.com/Support/Center
  • Ira Miller

    Re: Using modal popup edit form for updating multiple rows

    1/29/2010 3:01 PM
    • Not Ranked
    • Joined on 1/29/2010
    • Posts 1

    I am trying to do the same thing. Have an edit button to edit selected rows and update all selected rows with only information entered into a popup window. set xyz field for all selected rows to with a value of "abc"... I don't need to multi-row edit, just select rows in a gridview, press a button and get an interface to enter updated info for several of the fields then write the updated data to each selected row to the database.

    I've tried to go this direction but have been unsuccessful... Any more light you can shed?

  • Andy Tr (DevExpress)

    Re: Using modal popup edit form for updating multiple rows

    2/4/2010 2:32 AM
    • Top 50 Contributor
    • Joined on 5/7/2007
    • Posts 688

    Hello Ira,

    You can try adopting the following approach:

     

    ASPX:

        <form id="form1" runat="server">

            Select one or several grid rows and click the Edit button to modify row values.<br /><br />

     

            <dx:aspxgridview id="ASPxGridView1" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid" KeyFieldName="ID" OnCellEditorInitialize="ASPxGridView1_CellEditorInitialize" OnCustomCallback="ASPxGridView1_CustomCallback" OnRowUpdating="ASPxGridView1_RowUpdating" OnStartRowEditing="ASPxGridView1_StartRowEditing">

                <SettingsBehavior AllowMultiSelection="True"></SettingsBehavior>

                <Columns>

                    <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">

                    </dx:GridViewCommandColumn>

     

                    <dx:GridViewDataTextColumn FieldName="ID" VisibleIndex="1" ReadOnly="True">

                        <EditFormSettings Visible="False" />

                    </dx:GridViewDataTextColumn>

     

                    <dx:GridViewDataTextColumn FieldName="File" VisibleIndex="2">

                        <PropertiesTextEdit NullText="Type new value here">

                        </PropertiesTextEdit>

                    </dx:GridViewDataTextColumn>

     

                    <dx:GridViewDataTextColumn FieldName="Folder" VisibleIndex="3">

                        <PropertiesTextEdit NullText="Type new value here">

                        </PropertiesTextEdit>

                    </dx:GridViewDataTextColumn>

                </Columns>

                <SettingsEditing Mode="PopupEditForm" EditFormColumnCount="1" PopupEditFormModal="True" PopupEditFormHorizontalAlign="WindowCenter" PopupEditFormVerticalAlign="WindowCenter" />

            </dx:aspxgridview>

     

            <br />

     

            <dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Edit">

                <ClientSideEvents Click="function(s, e) {

                    if(grid.GetSelectedRowCount() == 0) alert('No grid row is selected.');

               else grid.PerformCallback('StartEditing');

                }" />

            </dx:ASPxButton>

     

        </form>

     

     

    Code-Behind:

        bool isEditing = false;

     

        protected void Page_Load(object sender, EventArgs e){

            if (!IsPostBack) Session["GridDataSource"] = CreateDataSource();

            ASPxGridView1.DataSource = Session["GridDataSource"] as DataTable;

            ASPxGridView1.DataBind();

        }

     

        private DataTable CreateDataSource(){

            DataTable dataTable = new DataTable("MyTable");

            dataTable.Columns.Add("ID", typeof(int));

            dataTable.Columns.Add("File", typeof(string));

            dataTable.Columns.Add("Folder", typeof(string));

            dataTable.PrimaryKey = new DataColumn[ { dataTable.Columns["ID"] };

            for (int i = 0; i < 15; i++){

                dataTable.Rows.Add(new object[ { i, "File" + i.ToString(), "Folder1" });

            }

            return dataTable;

        }

     

        protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e){

            ASPxGridView grid = sender as ASPxGridView;

            if (e.Parameters == "StartEditing") {

                //grid.StartEdit(grid.FindVisibleIndexByKeyValue(grid.GetSelectedFieldValues("ID")[0]));

                object lastSelectedRowKeyValue = grid.GetSelectedFieldValues("ID")[grid.Selection.Count - 1];

                int lastSelectedRowVisibleIndex = grid.FindVisibleIndexByKeyValue(lastSelectedRowKeyValue);

                grid.StartEdit(lastSelectedRowVisibleIndex);

            }

        }

     

        protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e){

            isEditing = true;

        }

     

        protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e){

            ASPxGridView grid = sender as ASPxGridView;

            if (!(grid.IsEditing && isEditing)) return;

            if (grid.Selection.Count == 1 || e.Column.ReadOnly) return;

            e.Editor.Value = IsCommonValueForAllSelectedRows(e.Column, e.Value) ? e.Value : null;

        }

     

        private bool IsCommonValueForAllSelectedRows(DevExpress.Web.ASPxGridView.GridViewDataColumn column, object value) {

            bool res = true;

            List<object> selectedRowValues = ASPxGridView1.GetSelectedFieldValues(column.FieldName);

            for (int i = 0; i < selectedRowValues.Count; i++){

                if (selectedRowValues[i].ToString() != value.ToString()){

                    res = false;

                    break;

                }

            }

            return res;

        }

     

        protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e){

            DataTable dt = Session["GridDataSource"] as DataTable;

     

            if (ASPxGridView1.Selection.Count == 1) {

                DataRow row = dt.Rows.Find(e.Keys["ID"]) as DataRow;

                row["File"] = e.NewValues["File"];

                row["Folder"] = e.NewValues["Folder"];

            }

     

            if (ASPxGridView1.Selection.Count > 1){

                List<object> selectedRowKeys = ASPxGridView1.GetSelectedFieldValues("ID");

                for (int i = 0; i < selectedRowKeys.Count; i++){

                    DataRow row = dt.Rows.Find(selectedRowKeys[i]) as DataRow;

                    row["File"] = (e.NewValues["File"] == null) ? row["File"] : e.NewValues["File"];

                    row["Folder"] = (e.NewValues["Folder"] == null) ? row["Folder"] : e.NewValues["Folder"];

                    continue;

                }

            }

     

            dt.AcceptChanges();

            Session["GridDataSource"] = dt;

     

            e.Cancel = true;

            ASPxGridView1.CancelEdit();

        }

    Thanks,
    R&D, ASP.NET Team, DevExpress
    Andrew

    P.S. This is a peer-to-peer forum. For technical support, contact our Support Team directly at http://www.devexpress.com/Support/Center/ to get a guaranteed reply within a business day.
    Filed under:
  • Andy Tr (DevExpress)

    Re: Using modal popup edit form for updating multiple rows

    2/4/2010 10:43 AM
    • Top 50 Contributor
    • Joined on 5/7/2007
    • Posts 688

    We've created a Code Central example based on this approach:

    http://www.devexpress.dev/Support/Center/p/E2026.aspx (How to edit multiple selected rows in a single Edit Form)

    Thanks,
    R&D, ASP.NET Team, DevExpress
    Andrew

    P.S. This is a peer-to-peer forum. For technical support, contact our Support Team directly at http://www.devexpress.com/Support/Center/ to get a guaranteed reply within a business day.
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.