Solved: “ASPxGridView component’s GetSelectedFieldValues method does not retrieving values”

One of the amazing components in DevExpress suite is ASPxGridView. It has a method called GetSelectedFieldValues for retrieving values which are selected by the user. However, you might run into a problem such as retrieving nothing!

Problem with getting selected values in the runtime?
Problem with getting selected values in the run time?

 

Here is the code that you use and fail retrieving data:

Default.aspx.cs:

List<object> fieldValues2 = gridSiteList.GetSelectedFieldValues(new string[] { "column1", "column2" });

Default.aspx:

<dx:ASPxGridView ID="gridSiteList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" Width="297px">
     <Columns>
          <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" Width="40px">
          </dx:GridViewCommandColumn>
          <dx:GridViewDataTextColumn FieldName="column1" VisibleIndex="1" Width="130px" Caption="Name">
          </dx:GridViewDataTextColumn>
          <dx:GridViewDataTextColumn FieldName="column2" VisibleIndex="2" Width="130px" Caption="IP">
          </dx:GridViewDataTextColumn>
     </Columns>
</dx:ASPxGridView>

Solution

ASPxGridView needs you to specify the name of a unique column to work correctly. In order to specify it, simply add this parameter into the component definition:

KeyFieldName="column1"

So, the edited ASPX code look like this:

<dx:ASPxGridView ID="gridSiteList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" KeyFieldName="column1" Width="297px">
     <Columns>
          <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" Width="40px">
          </dx:GridViewCommandColumn>
          <dx:GridViewDataTextColumn FieldName="column1" VisibleIndex="1" Width="130px" Caption="Name">
          </dx:GridViewDataTextColumn>
          <dx:GridViewDataTextColumn FieldName="column2" VisibleIndex="2" Width="130px" Caption="IP">
          </dx:GridViewDataTextColumn>
     </Columns>
</dx:ASPxGridView>

You don’t need to change anything in CS file.

Leave a Reply