Here is my datagrid
[HTML]<asp:datagrid id="dgReorgs" runat="server" OnSortCommand=" dgReorgs_SortCo mmand" OnPageIndexChan ged="dgReorgs_P ageIndexChanged "
PagerStyle-HorizontalAlign ="Center" PagerStyle-Mode="NumericPa ges" BorderWidth="0p x" CellSpacing="1" CellPadding="2" AllowSorting="T rue" ShowFooter="Tru e" AutoGenerateCol umns="False" PageSize="20"
AllowPaging="Tr ue" OnItemCommand=" dgReorgs_ItemCo mmand" PagerStyle-CssClass="Pagin gLink">
<Columns>
<asp:TemplateCo lumn>
<HeaderStyle HorizontalAlign ="Center"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
<HeaderTemplate >
<asp:ImageButto n ID="ibtnAdd" Runat="server" ImageUrl="image s/Add.gif" OnClick="ibtnAd d_Click" Visible="False" ></asp:ImageButton >
</HeaderTemplate>
<ItemTemplate >
<asp:ImageButto n ID="ibtnEdit" Visible="False" Runat="server" ImageUrl="image s/Edit.gif" CommandArgument ='<%# DataBinder.Eval (Container,"Dat aItem.record_id ")%>' CommandName="Ed it">
</asp:ImageButton >
</ItemTemplate>
</asp:TemplateCol umn>
<asp:BoundColum n DataField="offe r_posting_date" SortExpression= "offer_posting_ date" HeaderText="Off er Posting Date"
DataFormatStrin g="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="symb ol" SortExpression= "symbol" HeaderText="Sym bol" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="offe r_expiration_da te" SortExpression= "offer_expirati on_date" HeaderText="Exp iration Date"
DataFormatStrin g="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="note s" SortExpression= "notes" HeaderText="Not es" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle Width="180px" BackColor="Whit e" Wrap="True"></ItemStyle>
</asp:BoundColumn >
</Columns>
<PagerStyle HorizontalAlign ="Center" Mode="NumericPa ges"></PagerStyle>
</asp:datagrid>[/HTML]
The following statement is the key CommandArgument ='<%# DataBinder.Eval (Container,"Dat aItem.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.CommandArgume nt.
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