473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

nateraaaa
663 Recognized Expert Contributor
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
[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 >&nbsp;
</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
  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
Jun 11 '07 #1
1 13297
sugunavathy
2 New Member
it's super.but i need gridview coding.u know the coding post me.
Feb 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
11830
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be completed. here's the code: System.Data.OleDb.OleDbConnection hist_cnn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand hist_command = new System.Data.OleDb.OleDbCommand();
0
1554
by: Solomon Shaffer | last post by:
I have a datagrid that I am adding a template column to (containing a couple of linkbuttons) at runtime and am having some difficulty setting the commandargument or the linkbuttons. Does anyone know the syntax to databind the commandargument to the row through code. I am assumning I use the DataBinder.Eval method but I cannot get it going. Below is the code sample. Thanks Solomon public class KnowlagentDataGridTemplate : ITemplate
2
2665
by: jason | last post by:
i'm trying to set the commandargument property of a buttoncolumn in the DataGrid ItemBind event. it works fine in one instance, but in the latest page i've written it doesn't seem to work, and i can't see why. can anyone tell me why this chunk of code works: <asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 136px" runat="server" CellPadding="3" OnItemDataBound="DataGridBindEvent"...
1
6407
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and binding it to a repeater control. This repeater control has multiple text boxes and buttons. Can you please tell me how can i do paging in this case ? I'm posting my code below. The problem is that if i click on "AdjustThisAd" button, it opens...
0
2169
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be edited and then saved. Currently, after saving the edited transaction and returning to the check register, the repeater control table returns to the bottom. I would like the edited transaction to appear in the table instead. I know that a...
0
1882
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be edited and then saved. Currently, after saving the edited transaction and returning to the check register, the repeater control table returns to the bottom. I would like the edited transaction to appear in the table instead. I know that a JavaScript...
1
1509
by: Martin Pöpping | last post by:
Hello, I´ve a problem with the return values of a database (MS SQL) query. My code looks like this: foreach (DataRow row in dt.Rows) { this.fingerprints.Add(new DocumentFingerprint(row, row));
1
2112
by: Brett Wesoloski | last post by:
I am new to using template columns. I am just trying to create a data grid with a bound column and another column with a imagebutton in it. What I have always done in the past was to then create a datatable and bind that to the datagrid. Where is my code to bind the grid to the datatable. DataTable dt = new DataTable(); dt.Columns.Add("FundDescription");
8
4074
by: Lee | last post by:
guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). My question is if there is some other way I should do to speed up...
0
7981
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8284
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.