Can anyone out there tell me how to access data that is placed in a datagrid template column? I am coding in vb.net.
From what part of your code do you want to access it? If you want to access it while the grid is being databound, you can use ItemDataBound. Elsehwere, you'll need to iterate the items in the grid. Either way, you get to the specific column via the DataListItem.FindControl method. When you set up the template column, you put controls in the column and give them IDs. FindControl will return the control in the column, based on its id. You then get the data from the control.
MS MVP ASP.NET [VC++/MFC emeritus]
Old Dog Learns New Tricks
Preferred programming language: cuneiform on clay tablets
Once the datagrid has been populated and rendered, the user will choose a record by clicking the button control in that row. I then need to grab the data in the second column of the selected row.
In a regular column, I use: e.Item.Cells.Item(1).Text
The column's data that I need is a template column, as I had mentioned before. In reading your suggestion, you had mentioned placing controls in this template column and assigning an ID. I haven't done that. I simply made it a template. In the html, I direct this template column to a function that is stored in my code which simply concantinates two values from my dataset and then returns this value to populate the template column in my grid.
Seeing that I didn't actually place controls in the template and don't have ID's, can I still access the data how you had previously mentioned? And if so, would you be willing to be a little more descriptive on how the actual code would look?
Your help is greatly appreciated.
I figured it out. Thanks for the help.
I have similar problem. I'm trying to get access to template column when Update Command is fired, but my template column isn't editable - I only show data in it :
<asp:TemplateColumn Visible="False" HeaderText="wm_id">
Additional , this columns is hidden, that user cannot see it.
How can I read that column in DataGrid_UpdateCommand method?
Hi, your TemplateColumn is hidden, so you can't obtain the value in the normal way. You can store the ID as DataKeyField property of DataGrid so that you can refer to it in the UpdateCommand by calling
Hope this helps...
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
I see that DataKeys property is exactly what I needed (keeping hidden value in DataGrid's Rows).
You have showed me how to read from DataKeys, but I can't handle how to write to this Keys.
Could you give me short example how to assign eg. id of one DataTable to DataKeys?
Ok, after big struggle, I've asigned ho_id (DataTable) field with DataKey,
private void Load(string ss_id)
string q = "SELECT * FROM tbHoQRequirements WHERE ho_ss_id='"+ss_id+"'";
DataSet ds = helper.GetDataSet(q);
DataGridMain.DataKeyField = "ho_id";
private void DataGridMain_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
TextBoxInfo.Text = DataGridMain.DataKeys[e.Item.ItemIndex].ToString();
DataGridMain.EditItemIndex = -1;
but in my case, Dataset that filled DataGrid doesn't contain ho_id field and I must somehow populate DataKeyField in different way.