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


 
0
jcjcjc
11/16/2007 8:17:07 AM
asp.net.getting-started 91979 articles. 3 followers. Follow

16 Replies
513 Views

Similar Articles

[PageSpeed] 16


string txt = ((TextBox)(datagrid1.FindControl("Control1"))).Text;


Sincerely,
roy
0
royroy7
11/16/2007 9:14:01 AM

thank you, but I can't really understand ... can you explian more ?

 what is Control1 mean?

0
jcjcjc
11/16/2007 10:38:00 AM

 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 value from the Column in that Table

 How to get it? and what puzzling me is  the Column in Table have 2 rows ( A column have 2 rows each row have difference value .. )

how do I get value from this .. special Column?

 

thank you very much (sorry but I'm trying to explian my question clearly.. if I did make my question clearly please let me know)

 

 

0
jcjcjc
11/16/2007 10:42:21 AM

u will certainly do this

string txt = ((TextBox)(datagrid1.FindControl("Control1"))).Text;

in a foreach loop for all the rows u got in the datagrid but also in this foreach loop only u need to place another foreach or for loop for the rows that u recieve for that column (as explained by u above that u will recieve two such rows in that column's each row...)

 then u will be able to get the two values.....i hope u can understand this....


Regards,
Nitin Bansal
0
nitinba
11/16/2007 10:51:50 AM

 

jcjcjc:
in Item Template have a Table and Table have 3 columns

 Is your table Table web controller or HTML  

<table></table>

?

if it dosen't, make it as  

<asp:Table ID="Table1">

 
 then refer that table, i'l give u an example code.

 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Table _t = (Table)GridView1.Rows[e.RowIndex].FindControl("Table1");
}
 
 
Kelum W. Ganegoda (MCSD.NET)
:: Please mark as answer if you got it right ::
0
warnajith
11/16/2007 1:05:42 PM

hi thank you

can you explan more about this line please ?→ Table _t = (Table)GridView1.Rows[e.RowIndex].FindControl("Table1");

I think I don't have a GridView1 now . do I need to create one ?

I check my code it looks . (below)

   <ItemTemplate>
           <asp:Table id="Table7" runat="server" ForeColor="Transparent" Width="100%" CellPadding="0"
            Height="100%" HorizontalAlign="Center" GridLines="Vertical" CellSpacing="0">
            <asp:TableRow Width="100%" VerticalAlign="Middle" HorizontalAlign="Center">
             <asp:TableCell Text="代收" Width="30px" wrap="False" Font-Size="X-Small"></asp:TableCell>
             <asp:TableCell Width="100px" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"Account")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"CtcbBanSDTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"CtcbBanSDTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"OtherBanSDTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"OtherBanSDTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"GoodSDTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"GoodSDTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"BadSDTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"BadSDTotalAMT")%>'/>
            </asp:TableRow>
            <asp:TableRow VerticalAlign="Middle" HorizontalAlign="Center" BackColor="LightBlue">
             <asp:TableCell Text="代付" Width="30px" Wrap="False" Font-Size="X-Small"></asp:TableCell>
             <asp:TableCell Width="100px" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"SCAccount")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"CtcbBanSCTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"CtcbBanSCTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"OtherBanSCTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"OtherBanSCTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"GoodSCTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"GoodSCTotalAMT")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"BadSCTotal")%>'/>
             <asp:TableCell Width="70px" HorizontalAlign="Right" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"BadSCTotalAMT")%>'/>
            </asp:TableRow>
           </asp:Table>
          </ItemTemplate>
 
the  Text='<%# DataBinder.Eval(Container.DataItem,"Account")%>'/> and Text='<%# DataBinder.Eval(Container.DataItem,"SCAccount")%>'/>
2 values, one is Account anothr is SCAccount these 2 values in a same column..
How to get them? 
thank you..
' Font-Size="X-Small" wrap="False" Width="100px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" Width="100px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" Width="100px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" Width="100px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">' Font-Size="X-Small" wrap="False" HorizontalAlign="Right" Width="70px">
0
jcjcjc
11/16/2007 3:25:44 PM

Substitute the actual ID of your gridview instead of "GridView1", and since your table has ID "Table7", substitute "Table7" instead of "Table1".


-Tab Alleman
0
talleman
11/16/2007 8:31:26 PM

 thank you

sorry for asking again

I don't have gridview in my page now .. do you mean I need to creat a gridview by myself?

( I only have a DataGrid ..)

if I have to creat a gridview can you tell me how to ?

 

0
jcjcjc
11/17/2007 3:30:01 PM

Sorry 4 late I was in a weekend vacation , :-)

 

jcjcjc:
I don't have gridview in my page now .. do you mean I need to creat a gridview by myself?

no you dont want create a gridview. Since youe using DataGrid you can write this way, as an example

 

 protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
Table _t = (Table)e.Item.FindControl("Table7");

}

 
 FindControl() use to get a server control which is in a data grid row. Since it return a Control you have to cast it back to the actual control. Any control that inherits

INamingContainer interface can use this FindControl(string controlID);

 

By the way,

If you are sing .NET 2.0 use GridView instead of DataGrid. Data Control control exists only for backward compatibility. Also, GridView much more flexible than DataGrid.

 


Kelum W. Ganegoda (MCSD.NET)
:: Please mark as answer if you got it right ::
0
warnajith
11/19/2007 4:35:27 AM

thank you very much.. I don't know what's going wrong I just can't get the TAble  from DataGrid

 

my code now is below 

1    			Table tb;
2    			try
3    			{
4    				tb = (Table)dbgDetail.Items[e.Item.ItemIndex].Cells[5].FindControl("Table7");
5    				Response.Write("e.Item.ItemIndex:"+tb.Rows[0].Cells[1].Text+"&lt;br>");
6    				Response.Write("e.Item.ItemIndex:"+tb.Rows[1].Cells[1].Text+"&lt;br>");
7    
8    				string x = tb.Rows[0].Cells[5].Text;
9    				lblErr.Text = x;
10   			}
11   
12   			catch(Exception ex)
13   			{
14   				Response.Write("Exception Message:"+ex.Message+"&lt;br>");
15   			}
 
the result x have nothing  and tb have nothing .. I don't think I have got table from DataGrid.. 
 
can you please help ? I really can't find out what's going  wrong . 
 
thank you very much 
0
jcjcjc
11/19/2007 12:18:14 PM

this wrong,

jcjcjc:
tb = (Table)dbgDetail.Items[e.Item.ItemIndex].Cells[5].FindControl("Table7");
 

 you should correct this as 

tb = (Table)dbgDetail.Items[e.Item.ItemIndex].Cells[5].NamingContainer.FindControl("Table7");
 
 
Kelum W. Ganegoda (MCSD.NET)
:: Please mark as answer if you got it right ::
0
warnajith
11/19/2007 12:27:03 PM

 thank you , I am not sure if I got the Table ( no error message now)

I'm going to do the next ( retrieve value from the Table I just get from DataGrid)

the Table is like this

                                          <asp:Table id="Table7" runat="server" ForeColor="Transparent" Width="100%" CellPadding="0"
                                                Height="100%" HorizontalAlign="Center" GridLines="Vertical" CellSpacing="0">
                                                <asp:TableRow Width="100%" VerticalAlign="Middle" HorizontalAlign="Center">
                                                    <asp:TableCell Text="代收" Width="30px" wrap="False" Font-Size="X-Small"></asp:TableCell>
                                                    <asp:TableCell Width="100px" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"Account")%>'/>
                                                   </asp:TableRow>
                                                <asp:TableRow VerticalAlign="Middle" HorizontalAlign="Center" BackColor="LightBlue">
                                                    <asp:TableCell Text="代付" Width="30px" Wrap="False" Font-Size="X-Small"></asp:TableCell>
                                                    <asp:TableCell Width="100px" wrap="False" Font-Size="X-Small" Text='<%# DataBinder.Eval(Container.DataItem,"SCAccount")%>'/>
                                                </asp:TableRow>
                                            </asp:Table>

 

if I need to get value of  Account and SCAccount ( both DataBinder.Eval with column)

is the same way use foreach to loop the table's columns? thank you 

0
jcjcjc
11/20/2007 2:08:48 AM

 hi there, thank you very much , I've resolve my problem

can you please let me know what is the difference between use NamingContainer and use FindControl?

( it's you teach me NamingContainer or I have never know NamingContainer)

when should I use NamingContainer and when should I use FindControl?

 

appreciate with your help, thank you very much  

 

 

 

0
jcjcjc
11/20/2007 2:44:05 AM

 now u dont need to call again the NamingContainer for the table. Just refer the table instance and get the values as normal.


Kelum W. Ganegoda (MCSD.NET)
:: Please mark as answer if you got it right ::
0
warnajith
11/20/2007 10:21:00 AM

 NamingContainer refers the parent container control for a child control. Page is a NamingContainer for all child controls. Controls generate a NamingContainer by implementing the INamingContainer interface. 

Since your table is in the grid, it;s naming container is DataGrid,not grid's rows or columns. Because, DataGrid implements the INamingContainer interface. If we go upward to the hieracy, datagrid's naming container is Page.

 In the above code, you are referring the table control by the datagrid's rows and cells. If your referring that table  by datagrid you don't have to call NamingContainer again.
 


Kelum W. Ganegoda (MCSD.NET)
:: Please mark as answer if you got it right ::
0
warnajith
11/20/2007 10:58:42 AM

 got it!

 

thanks for you teach me this class,

really thanks for your help! 

thank you very much  Big Smile

0
jcjcjc
11/21/2007 3:57:35 AM
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...

POST GET GET GET GET... WTH?
Hi, I'm at a loss to explain this... I have an ASPX page that I can't always navigate to. When I developed the app (it's Starter Kit Portal-based), I did it on my LAN and have had no trouble accessing the page. However, upon deployment, I see the following behavior: 1. POST request goes out looking for MyForm.aspx 2. GET request for MyForm.aspx 3. GET request for MyForm.aspx 4. ... X. GET request Sometimes, the page finally loads. Other times, it just ends up blank. Many times, it doesn't load at ALL the first POST - I just get a "The pa...

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

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

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

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 the Cell value of datagrid hyperlink column
hi, i have a datagrid with hyplerlink column,bound column and checkbox column. when i check the checkbox i can get the row cell value in boundColumn but not in hyperlinkColumn, it return a null string.. please refer to my sample code:foreach(DataGridItem item in this.DataGrid1.Items) { CheckBox chk = (CheckBox)item.FindControl("checkbox");if(chk.Checked) { chekedData[0] = item.Cells[2].Text.ToString();   // this is the hyperlinkColumnstring hg0 = item.Cells[2].Text.ToString(); // this part is my checking if theres a data... chekedData[1] = item.Cells[3]...

Get Value from template column textbox in Datagrid
Sorry to come back with this request, but I am unable to get the correct info on how to access a value in a datagrid. The column in question is a textbox. I am trying to edit the row and use the value from the textbox (LocationID) as a filter for an SQL statement that will load a DDL for that location. When the Edit is executed I get the correct e.item.index value but don't know where to go from there. It has been suggested that I need to rebind the grid once in edit and then I should be able to get the LocationID, create the SQL and rebind before displaying the grid for editting. It has be...

Web resources about - How to Get Value from a Column of ItemTemplate of a DataGrid? - asp.net.getting-started

Using GridView ItemTemplate and ItemTemplateSelector at the same time
If you are building a XAML based WinRT application which uses a GridView it is very likely you are going to use an ItemTemplate style OR use ...

Index - 2,000 Things You Should Know About WPF
Below is a list of all posts created so far for the 2,000 Things You Should Know About WPF blog. Total number of posts = 1,077 Animation #7 - ...

My WinJS Control Template
I've created my own custom VS Item Template for creating new WinJS custom controls. Click here to download the template Installation: Save the ...

Silverlight Toolkit - Issues
Silverlight Toolkit and Silverlight for Windows Phone Toolkit are a set of Microsoft components for developers.

EFG Hermes
One of the leading Investment banks in the MENA region offering banking services to retail and institutional clients. Our services include retail ...

Silverlight AutoCompleteBox and MVVM
…stumbling in the footsteps of greatness

Microsoft Delivers Visual Basic Tools for Windows Phone 7 Development
With this new Community Technology Preview, Visual Basic developers can create Windows Phone 7 apps.

A jQuery Flickr Feed Plugin
This jQuery Flickr plugin allows you to pull a public Flickr feed and display it however you like. Compatible with lightbox, colorbox, cycle, ...

Hands on Windows 8 development: Twitter and hyperlink hassles
I have been messing around with a Windows 8 app to present content from ITWriting.com in an app, mainly as a learning exercise. I came up with ...

MSDN Magazine: Windows Phone 7 Apps - Build Data-Driven Apps with Microsoft Azure and Windows Phone 7 ...
In the last 30 years, we’ve seen an explosion in the computer hardware industry. From mainframes to desktop computers to handheld devices, the ...

Resources last updated: 1/4/2016 6:53:35 AM