Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can be sure the order of items in your data source has not changed, you can use the CommandArgument property of the CommandEventArgs.</p> <p>A more robust method, however,is to use the DataKeys/SelectedDataKey properties of the GridView. The only caveat is that your command must be of type "Select" (so, by default RowCommand will not have access to the DataKey).</p> <p>Assuming you have some uniqueness in the entities comprising your list, you can set one or more key property names in the GridView's DataKeys property. When the selected item in the GridView is set, you can retrieve your key value(s) and locate the item in your bound list. This method gets you out of the problem of having the ordinal position in the GridView not matching the ordinal position of your element in the data source.</p> <p>Example:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" DataKeyNames="Name" onrowcommand="GridView1_RowCommand1" onselectedindexchanged="GridView1_SelectedIndexChanged"&gt; &lt;/asp:GridView&gt; </code></pre> <p>Then the code-behind (or inline) for the Page would be something like:</p> <pre><code>protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { // Value is the Name property of the selected row's bound object. string foo = GridView1.SelectedDataKey.Value as string; } </code></pre> <p>Another choice would be to go spelunking in the Rows collection of the GridView, fetching values a column at a time by getting control values, but that's not recommended unless you have to.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload