Getting column values that are not shown in the datagrid


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.

   But how can I access it??

    - e.Item.DataItem is null - doesn't work
    - (DataView) myDataGrid.DataSource is also null - doesn'T work either :-(

There must be a simple solution, but - alas - I don't find it.
A hint would be highly appreciated.


6/14/2005 6:21:00 PM 72751 articles. 3 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 46

Show me your Datagrids Html and the function BindGrid.

Sonu Kapoor [MVP]
Feel free to ask me any .NET question | Ajax Data Controls Forum
6/14/2005 6:28:36 PM


thank u for your effort! The example was just created on scratch, so there's no real code there.

The essence is:

In the BindGrid function is a column called "AnotherID" that is NOT shown in the DataGrid, e.g. 

        <asp:label Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "AnotherID" )) %>' enableViewState="false" runat="server" />

is NOT used, since I dont want / need to SHOW this column

But in OnItemCommand function, I want to access the value for the selected row for the column "AnotherID".

I hope this makes my problem clear.


6/14/2005 6:55:21 PM
Is AnotherID the PrimaryKey? If so you can use the DataKeyField property and set it to the AnotherID. To retrieve you could do: dg.DataKeys(e.item.ItemIndex)
Sonu Kapoor [MVP]
Feel free to ask me any .NET question | Ajax Data Controls Forum
6/14/2005 7:02:37 PM
Nope, it isn't.

My work-around has always been to integrate "AnotherID" into the DAtaGrid but set visible=false,
thus, it is not shown but I can still access it. But I thought that maybe there was a cleaner approach.
6/15/2005 7:56:44 AM
You can try to create a linkbutton/button in the <asp:TemplateColumn that fires the ItemCommand event. Assign the AnotherID to the CommandArgument property of the LinkButton/Button in the  ItemDataBound event. Then, get the value of the CommandArgument in the ItemCommand event. By that, you could remove the AnotherID column, which is always hidden behind the scene.

protected void myDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e)
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            LinkButton lbtnName = (LinkButton)e.Item.FindControl("<your linkbutton ID>");

            lbtnName.CommandArgument = DataBinder.Eval(e.Item.DataItem,"AnotherID").ToString();

protected void myDataGrid_ItemCommand(Object sender, DataGridCommandEventArgs e)
LinkButton lbtnFirstname = (LinkButton)e.Item.FindControl("lbtnFirstname");

      string strAnotherID = lbtnFirstname.CommandArgument.ToString();

It would be something like that. Hope it helps you solve the problem. ;p

Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog :
6/15/2005 8:51:22 AM

Since i am a developer, i am writing the code in Create a template column for "AnotherID" and hide it in Datagrid. In the ItemDataBound event, assume ur column "AnotherID" in Datagrid has a label called lblAnotherID then

Dim strAnotherID As String

lblAnotherID = CType(e.Item.FindControl("lblAnotherID"), Label)

the above code will give u the value of AnotherID column in the string strAnotherID.

Hope this solves your problem. Do let me know.

6/15/2005 11:48:48 AM

thx a lot!

The last two posts are excellent and both solve the probem!
6/15/2005 12:20:00 PM

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...

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: 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....

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 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 to get the value out of the HyperLink column in the datagrid
I need to get the text which is displayed in the hyperlink column. I tried many ways but always it returns empty "". This is little bit of my C# code: foreach(DataGridItem dgi in myDataGrid.Items)             {                 CheckBox myCheckBox = (CheckBox)dgi.Cells[3].Controls[1];                 HyperLinkColumn hyper = (HyperLinkColumn) myDataGrid.Columns[1];          ...

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 t...

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 with then write like this  ID=e.Item.Cells[6].Text.Trim()   Ramesh Can you please be more specific regarding the type of data you...

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...

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 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 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 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. ...

How to Get Value from a Column of ItemTemplate of a DataGrid?
in my DataGrid column[5] is a Template Column in Item Template have a Table and Table have 3 columns How to get the Value from the Table in a Item Template   of DataGrid?  thank you string txt = ((TextBox)(datagrid1.FindControl("Control1"))).Text;Sincerely, roy thank you, but I can't really understand ... can you explian more ?  what is Control1 mean?  sorry let me explain my question  A datagrid have a Template Column and in the Template Column have a Table in the Table have many columns, I need to get a v...

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") %>              ...

Web resources about - Getting column values that are not shown in the datagrid -

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 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: 11/21/2015 4:58:26 PM