Skip to main content
Tab

ASPxGridBase.GetSelectedFieldValues(String[]) Method

Returns the field values of selected data items (rows, cards, or records).

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.2.dll

Declaration

public List<object> GetSelectedFieldValues(
    params string[] fieldNames
)

Parameters

Name Type Description
fieldNames String[]

The names of data source fields.

Returns

Type Description
List<Object>

The list of field values.

Remarks

The GetSelectedFieldValues method allows you to get a single or multiple column values of selected rows.

<dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="CustomerID" AutoGenerateColumns="False">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="ContactName" VisibleIndex="0" />
        <%--...--%>
        <dx:GridViewDataTextColumn FieldName="Country" VisibleIndex="4" />
    </Columns>
    <SettingsBehavior AllowSelectByRowClick="true" />
</dx:ASPxGridView>
// Get a single field value
var selectItems = ASPxGridView1.GetSelectedFieldValues("ContactName");

// Get multiple field values
var selectItems = ASPxGridView1.GetSelectedFieldValues("ContactName", "Country");

View Example: How to get column values of multiple selected rows

To get the values of selected and filtered data items, call the GetSelectedFieldValues(String[]) method.

Limitation

  • When cell merge is enabled, the GetSelectedFieldValues property is not in effect.

Example

The code sample below calls the GetSelectedFieldValues method to obtain selected rows and deletes them from the grid.

View Example: ASPxGridView - How to Delete Selected Rows in a Grid

<dx:ASPxGridView ID="grid" KeyFieldName="ID" runat="server" AutoGenerateColumns="False"
    ClientInstanceName="grid" OnCustomCallback="gridView_CustomCallback" >
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0" ShowSelectCheckbox="True">
            <FooterTemplate>
                <dx:ASPxButton ID="buttonDel" AutoPostBack="false" runat="server" Text="Delete">
                    <ClientSideEvents Click="OnClickButtonDel" />
                </dx:ASPxButton>
            </FooterTemplate>
        </dx:GridViewCommandColumn>
        <%--...--%>
    </Columns>
    <Settings ShowFooter="True" />
</dx:ASPxGridView>
function OnClickButtonDel(s, e) {
    grid.PerformCallback('Delete');
}
protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) {
    if(e.Parameters == "Delete") {
        table = GetTable();
        List<Object> selectItems = grid.GetSelectedFieldValues("ID");
        foreach(object selectItemId in selectItems) {
            table.Rows.Remove(table.Rows.Find(selectItemId));
        }
        grid.DataBind();
        grid.Selection.UnselectAll();
    }
}

Get the Values of Complex Fields

The GetSelectedFieldValues method returns column values only. So, if a grid is bound to a collection of complex objects (for example, KeyFieldName="Product.Category.CategoryName"), create a hidden column for this complex field. The grid does not render hidden columns, but allows you to get their data on the server side.

Web Forms Example

ASPxGridView1.Columns.Add("SubRegionInfo.ID").Visible = false;

MVC Example

settings.Columns.Add("SubRegionInfo.ID").Visible = false;

Get Values in Server Mode

To get the key values of selected and filtered rows in Server Mode, set the SelectionStoringMode property to DataIntegrityOptimized.

If you assign the composite key to the KeyFieldName property, you can pass each key field name as a parameter to the GetSelectedFieldValues method.

In Server Mode, the GetSelectedFieldValues method does not return the non-key values of selected and filtered rows. Use the key values to obtain other values from a database directly:

// For a single key value
query.FirstOrDefault(q => q.KeyProperty == "MyKeyValue")

//For a composite key value
query.FirstOrDefault(q => q.KeyProperty1 == "MyKeyValue1" && q.KeyProperty2 == "MyKeyValue2" && ...)

Online Example

View Example: How to move selected rows between grid controls

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetSelectedFieldValues(String[]) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also