Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Using CommandArgument to find the database table record_id for a datagrid row

Written by nateraaaa, June 11th, 2007
While working on a recent project I discovered how useful the CommandArgument property can be. I needed to determine the record_id of a row displayed in my datagrid. When the user clicked the Edit ImageButton I needed to redirect the user to the Edit page where the data on the page would be prepopulated with data from the database. I tried several things without success then discovered that the CommandArgument property could be used for this task.

Here is my datagrid
Code: ( text )
  1. <asp:datagrid id="dgReorgs" runat="server" OnSortCommand="dgReorgs_SortCommand" OnPageIndexChanged="dgReorgs_PageIndexChanged"
  2. PagerStyle-HorizontalAlign="Center" PagerStyle-Mode="NumericPages" BorderWidth="0px" CellSpacing="1" CellPadding="2" AllowSorting="True" ShowFooter="True" AutoGenerateColumns="False" PageSize="20"
  3. AllowPaging="True" OnItemCommand="dgReorgs_ItemCommand" PagerStyle-CssClass="PagingLink">
  4. <Columns>
  5. <asp:TemplateColumn>
  6. <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
  7. <ItemStyle HorizontalAlign="Center"></ItemStyle>
  8. <HeaderTemplate>
  9. <asp:ImageButton ID="ibtnAdd" Runat="server" ImageUrl="images/Add.gif" OnClick="ibtnAdd_Click" Visible="False"></asp:ImageButton>
  10. </HeaderTemplate>
  11. <ItemTemplate>
  12. <asp:ImageButton ID="ibtnEdit" Visible="False" Runat="server" ImageUrl="images/Edit.gif" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.record_id")%>' CommandName="Edit">
  13. </asp:ImageButton>&nbsp;
  14. </ItemTemplate>
  15. </asp:TemplateColumn>
  16. <asp:BoundColumn DataField="offer_posting_date" SortExpression="offer_posting_date" HeaderText="Offer Posting Date"
  17. DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
  18. <HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
  19. <ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
  20. </asp:BoundColumn>
  21. <asp:BoundColumn DataField="symbol" SortExpression="symbol" HeaderText="Symbol" HeaderStyle-Font-Size="10">
  22. <HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
  23. <ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
  24. </asp:BoundColumn>
  25. <asp:BoundColumn DataField="offer_expiration_date" SortExpression="offer_expiration_date" HeaderText="Expiration Date"
  26. DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
  27. <HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
  28. <ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
  29. </asp:BoundColumn>
  30. <asp:BoundColumn DataField="notes" SortExpression="notes" HeaderText="Notes" HeaderStyle-Font-Size="10">
  31. <HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
  32. <ItemStyle Width="180px" BackColor="White" Wrap="True"></ItemStyle>
  33. </asp:BoundColumn>
  34. </Columns>
  35. <PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
  36. </asp:datagrid>


The following statement is the key CommandArgument='<%# DataBinder.Eval(Container,"DataItem.record_id")%>' (see line 12 in the code). NOTE: This statement will not work if the datasource of your datagrid does not contain a record_id column.

Now in the code behind.
I use the ItemCommand event of the datagrid to get the record_id of the row that has been selected for editing. In this event I have to check to see if the Item is part of the datagrid header section or if it is part of the datagrid pager section. If it is not then I can redirect the page to the Edit page and append the record_id as a querystring using e.CommandArgument.
Code: ( text )
  1. public void dgReorgs_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
  2. {
  3. if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager)
  4. {
  5. if(e.CommandName == "Edit")
  6. {
  7. Response.Redirect("EditReorg.aspx?value=" + e.CommandArgument);
  8. }
  9. }
  10. }


Then in the page_load event of the Edit page I will call a query that will get me the data for this row and then prepopulate the controls on the page with data.

I hope this code will help some of you understand how to use the CommandArgument property.

Nathan

1 Comment Posted ( Post your comment )
sugunavathy / February 16th, 2008 09:05 AM
it's super.but i need gridview coding.u know the coding post me.

Stats:
Views: 2136
Comments: 1