Here is my datagrid
[HTML]<asp:datagrid id="dgReorgs" runat="server" OnSortCommand="dgReorgs_SortCommand" OnPageIndexChanged="dgReorgs_PageIndexChanged"
PagerStyle-HorizontalAlign="Center" PagerStyle-Mode="NumericPages" BorderWidth="0px" CellSpacing="1" CellPadding="2" AllowSorting="True" ShowFooter="True" AutoGenerateColumns="False" PageSize="20"
AllowPaging="True" OnItemCommand="dgReorgs_ItemCommand" PagerStyle-CssClass="PagingLink">
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<asp:ImageButton ID="ibtnAdd" Runat="server" ImageUrl="images/Add.gif" OnClick="ibtnAdd_Click" Visible="False"></asp:ImageButton>
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" Visible="False" Runat="server" ImageUrl="images/Edit.gif" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.record_id")%>' CommandName="Edit">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="offer_posting_date" SortExpression="offer_posting_date" HeaderText="Offer Posting Date"
DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="symbol" SortExpression="symbol" HeaderText="Symbol" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="offer_expiration_date" SortExpression="offer_expiration_date" HeaderText="Expiration Date"
DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="notes" SortExpression="notes" HeaderText="Notes" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle Width="180px" BackColor="White" Wrap="True"></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
</asp:datagrid>[/HTML]
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.
Expand|Select|Wrap|Line Numbers
- public void dgReorgs_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
- {
- if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager)
- {
- if(e.CommandName == "Edit")
- {
- Response.Redirect("EditReorg.aspx?value=" + e.CommandArgument);
- }
- }
- }
I hope this code will help some of you understand how to use the CommandArgument property.
Nathan