Getting value from a column in a datagrid for record

I have an ASP.NET page with a datagrid on it.  It has an Edit button column on it which I am changing what happens when you click on it.  Originally it opens up editing boxes in the datagrid, I am moving the editing function to an external form.

 I have modified the Edit command and am able to get the Index number of the record I clicked on with  e.Item.ItemIndex or DataGrid1.SelectedIndex but the problem is that I need the ID of the record, which is located in a hidden column  (Column 1).

 What would be the best means to be able to extract the ID from the record selected in the datagrid?

 Thanks,

~Drew

 


To error is human
To really foul up requires a computer!
0
dragonbite
4/14/2009 1:41:39 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
539 Views

Similar Articles

[PageSpeed] 8

Place the name of the ID field into the DataGrid.DataKeyField property and then you can extract that field at any time by simply accessing the DataGrid.DataKeys collection and giving it the row index of the row in which are trying to retrieve the ID. Something like this: 

protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
    string id = DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();
}

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
4/14/2009 3:06:07 PM

Try below code

Protected Sub grdView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
    ' CommandName to determine which button hasclicked.
    If e.CommandName = "btnEdit_Click" Then
      
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
      
        Dim ID As Integer = grdView.DataKeys(index).Value.ToString()

    End If
End Sub


Please mark it as answer if it resolves the issue.

visit: http://technicalsol.blogspot.com
0
sumitd
4/14/2009 3:24:49 PM

I actually managed getting the data from this, which I found elsewhere in the application. I can do this for all of the fields in the data grid row I want and use these values to populate the form without having to go back to the database. 

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand


        Dim intCovenantId As Integer = CType(e.Item.FindControl("lblCovenantId"), Label).Text
        Dim strNotes As Integer = CType(e.Item.FindControl("lblNotes"), Label).Text


End Sub

To error is human
To really foul up requires a computer!
0
dragonbite
4/16/2009 4:27:53 PM
Reply:

Similar Artilces:

Get the column value from a DataGrid
 Hi,I have a datagrid with 6 columns. I am trying to retrieve the value of a column and pass it to a variable. I am not sure what I doing wrong here since the code I am using is not pulling any value from the column.  This is an example of what I am trying to do. Dim column_value As Stringcolumn_value = DataGrid1.Items(0).Cells.Item(4).Text                        Please correct me if I am wrong.   Thanks.    Use a GridView control instea...

query to get last 2 column value as value for newly inserted records.
 hi here is the problem,i need insert statement which do the following.the query have to first 1-get the max yearid and then 2- put the value of  currenyear  in lastyear3-and the value of next year in currentyear,4- increment the value of yearid by 1.ID  name    yearid    lastyear    currentyear   next year1      A        2          100           400   &nb...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

How to get values from 3 columns of my datagrid
Hi, I have a dg that when the page loads it opens in edit mode, all rows. I am doing this by using template columns in my dg and by placing editable items( text box, check box list, etc) in the item template area so when the datagrid loads it automatically have this controls redy for the user's input, some other columns are actually binding from a stored proc. The problem is, I don't know how to retrieve the values that exist in those controls.Sample code: For i = 0 To dgProductItems.Items.Count - 1 objPDF = CType(dgProductItems.Items(1).Cells(6).Controls(0), RadioButtonList) If objPDF.Se...

How to get a datagrid column value into a variable?
Hi all, I have a datagrid which contains a column [Batch_Num] in the 7th column. In dgAppStatus_Delete(), I want to delete a row based on the employID and Batch_Num, where employID is the DataKeyField in the datagrid. However, how can I get the value of Batch_Num???? Please help. Thanks. -------------------------------- Sub BindData() Dim strSql As New SqlCommand Dim dtrDataGrid As SqlDataReader Dim dataAdapter As New SqlDataAdapter Dim dataSet As New DataSet Dim Source As DataView Try 'Ini...

How can I get the value of a column in a datagrid?
Hello. I have a datagird with textboxes on a column item template, I also have 2 other columns that are hidden and I want to retrieve his value. But I havent found how to do it. Thanks very much in advance This is the code that loops through all rows. find the textboxes controls Private Sub btnguardarplandesarrollo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardarplandesarrollo.Click Page.Validate() If Page.IsValid Then Dim cont As Int32 = 0 Dim di As DataGridItem For Each di In dgplandesarrollo.Items Dim responsabili...

Getting column values that are not shown in the datagrid
Hi,I've had this litte problem several times and couldn't really solve it:void BindGrid(){   ...   string sql = "select ID, Name, AnotherID from ....";   ...}The columns "ID" and "Name" are shown by the DataGrid, the column "AnotherID" is not shown.void OnItem(object Sender, DataGridCommandEventArgs e){   now, here I want to access "AnotherID" for the selected row. someting like "((TextBox)  e.Item.Cells[1].Controls[1]).Text ));" will not work, of course,   since "AnotherID" is not shown in the DataGrid. &nb...

HOW TO GET THE VALUES OF AN SPECIFIC COLUMN IN A DATAGRID
HI, Can you tell me how can i get the value of a column or columns which row i select ? thanks Patrick Hi,  write the follwing code..  ID=e.Item.Cells[6].Text.Trim(); here we wil get the id which we have selected ...6 is nothing but it is the 6th field we have bounded to the datagrid...   Ramesh   THIS CODE IS NOT HELPING ME, BECAUSE I-M USING VISUAL WEB DEVELOPER THANKS hi , if u are using asp.net with vb.net then write like this  ID=e.Item.Cells[6].Text.Trim()   Ramesh Can you please be more specific regarding the type of data you...

Getting the value of a certain column in the datagrid
Its quite simple what I'm trying to do but its never that simple when u don't know how. I just need to get the value of a certain column. noteID = (int) C1WebGrid1.DataKeys[e.Item.ItemIndex]; This lets me get the column id which i use to tell the db that this note is to status is to be set as read, this then moves the row into another grid. Problem is I need to run the read/unread through an if statement so i can toggle the row has read/unread. If I know that it been returned as unread then I toggle it to read and vice versa. I just need to that column value from the grid. ...

Getting column value from a gridview control
I have a grid view control and has one datakey and other columns. In my grid view I have an Edit Control and a Delete Control. 1) When I press Edit control, I am getting datakey value as expected. But I am not getting other column name i.e. pdf_filename 2) when I press Delete control, I should get the datakey value. But I am not getting I am using c#  Please help me Hi vmk, Could you be a littlemore elaborate? like how have you bound your grid? 1.Columns autogenerated 2.Using TemplateFields 3.Using BoundFields.Regards,NaveenPlease remember to click Mark as Answer on the pos...

Getting the value out of the Template Column in Datagrid
)'> How can I get the Value of the BatchID. )'><ItemTemplate>                                                                                 <%# DataBinder.Eval(Container.DataItem, "BatchID") %>              ...

HOw to get the control from a ItemTemplate Column of DataGrid
in a DataGrid have a Templated Column  (this Templated Column is Column[5]) and in this Templated Column have a Table How to get the Table in DataGrid's ItemTemplated Column? ( the id of  Table in Templated is Table7)   I think the code may be first I have to  →  DataTable dt = new DateTable() dt = ??  ( have no idea how to let the dt = Table 7)   please help.. thank you very much Are you trying to access the table7 in an event? In which context are you trying to access it ?  - Surya I'm puzzling how to get a value which is in a co...

Web resources about - Getting value from a column in a datagrid for record - asp.net.presentation-controls

Want to preview our new DataGrid for Xamarin.Forms?
tl;dr Zumero.DataGrid is a Xamarin.Forms control for displaying data in rows and columns. If you would be interested in testing and previewing ...

Mad King DataGrid
This post is about scrolling in WPF and the egocentric DataGrid control. I’ll give the project background but if all you’re interested in is ...

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 295 ...

Microsoft Silverlight - Wikipedia, the free encyclopedia
Microsoft Silverlight is an application framework for writing and running rich Internet applications , with features and purposes similar to ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Casaba Security » Products » X5s Testing Tool For Finding XSS Vulnerabilities
Security analysis, consulting, development and testing partners who understand your business strategy.

CodeGuru - Microsoft developers related ideas, articles, tips, tricks, comments, downloads, and so much ...
CodeGuru is where developers can come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and so much more related ...

Caliburn Micro: WPF, Silverlight, WP7 and WinRT/Metro made easy.
A small, yet powerful framework designed for WPF, Silverlight and WP7. It implements a variety of UI patterns for solving real-world problems. ...

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 11 Jul 2012

license-discuss@opensource.org: 11636: first draft of license proliferation committee report
FYI,the first draft report of the OSI's License Proliferation Committee.To join the OSI's license proliferation discuss email list, send anemail ...

Resources last updated: 12/28/2015 7:56:01 AM