Giter Site home page Giter Site logo

devexpress-examples / blazor-dxgrid-disable-editing-for-several-rows Goto Github PK

View Code? Open in Web Editor NEW
0.0 34.0 0.0 347 KB

Allow users to edit only the rows whose field values match your condition.

License: Other

HTML 78.99% C# 21.01%
blazor data-editor dxgrid editing dxeditors

blazor-dxgrid-disable-editing-for-several-rows's Introduction

Grid for Blazor - Restrict data editing to rows that match specific conditions

This example disables our Blazor Grid’s Edit button whenever the Date field value is less than or equal to the current date.

Blazor DxGrid disable row editing

Overview

Follow the steps below to enable row editing for field values that match a specific condition.

1. Create a custom Edit button

Declare the DxGridCommandColumn object in the Columns template to display a command column. The command column displays predefined New, Edit, and Delete command buttons. Define the column's cell display template to implement a custom Edit button.

<DxGrid @ref="myGrid" Data="@forecasts">
    <Columns>
        <DxGridCommandColumn NewButtonVisible=false>
            <CellDisplayTemplate>
                <DxButton Text="Edit"/>
            </CellDisplayTemplate>
        </DxGridCommandColumn>
    </Columns>
</DxGrid>

2. Customize Edit button behaviour

Use the cell display template's context parameter to access the grid object and the processed data item. Set the Edit button's Enabled property to true or false based on the data item's field values. Call the grid's StartEditDataItemAsync method to display an edit form when a user clicks the Edit button.

<CellDisplayTemplate>
    @{
        var date = context.DataItem != null ? (context.DataItem as WeatherForecast).Date : DateTime.Now;
            <DxButton Text="Edit"
                      Click="() => myGrid.StartEditDataItemAsync(context.DataItem)" 
                      Enabled="@(date <= DateTime.Now)" />
    }
</CellDisplayTemplate>

3. Define edit form template

The default edit form displays predefined Save and Cancel buttons. Use the edit form template to define the edit form's content. This example uses our Blazor DxFormLayout to display automatically generated editors in the edit form for all editable data columns.

<EditFormTemplate Context="editFormContext">
    <DxFormLayout>
        <DxFormLayoutItem Caption="Date:">
            @editFormContext.GetEditor("Date")
        </DxFormLayoutItem>
        <DxFormLayoutItem Caption="Summary:">
            @editFormContext.GetEditor("Summary")
        </DxFormLayoutItem>
        <DxFormLayoutItem Caption="Temperature (C):">
            @editFormContext.GetEditor("TemperatureC")
        </DxFormLayoutItem>
        <DxFormLayoutItem Caption="Temperature (F):">
            @editFormContext.GetEditor("TemperatureF")
        </DxFormLayoutItem>
    </DxFormLayout>
</EditFormTemplate>

4. Save changes and reload data

The EditModelSaving event fires when a user submits an edit form and allows you to save changes. Use the following event argument properties to post changes to the data source:

  • The EditModel property returns the edit model that stores all changes.
  • The DataItem property returns the proccesed data item.

The Blazor Grid automatically reloads its data after you post updates to the data source.

<DxGrid @ref="myGrid" Data="@forecasts" EditModelSaving="OnEditModelSaving">
    <!-- ... -->
</DxGrid>

@code {
    void OnEditModelSaving(GridEditModelSavingEventArgs e) {
        WeatherForecast dataItem = e.DataItem as WeatherForecast;
        WeatherForecast editModel = e.EditModel as WeatherForecast;
        dataItem.Date = editModel.Date;
        dataItem.TemperatureC = editModel.TemperatureC;
        dataItem.Summary = editModel.Summary;
    }
}

Files to Review

Documentation

More Examples

blazor-dxgrid-disable-editing-for-several-rows's People

Contributors

khamlyuk avatar svetlanamikheeva avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.