accessing data in a datagrid template column

Can anyone out there tell me how to access data that is placed in a datagrid template column?  I am coding in vb.net.
0
BroncosRule
3/2/2004 1:18:33 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

7 Replies
387 Views

Similar Articles

[PageSpeed] 28

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


MS MVP ASP.NET [VC++/MFC emeritus]


Old Dog Learns New Tricks

Preferred programming language: cuneiform on clay tablets
0
JimRoss
3/2/2004 1:42:54 PM
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.
0
BroncosRule
3/2/2004 3:33:47 PM
I figured it out.  Thanks for the help.
0
BroncosRule
3/2/2004 4:18:46 PM
Hi,
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">
                  <ItemTemplate>
                    <%#Get_Wm_Id()%>
                  </ItemTemplate>
</asp:TemplateColumn>

Additional , this columns is hidden, that  user cannot see it.

How can I read that column in DataGrid_UpdateCommand method?
0
lupina
11/5/2005 6:03:26 PM
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

DataGrid1.DataKeys[e.Item.ItemIndex].ToString();


Hope this helps...
Alvin Chooi
Microsoft ASP.NETâ„¢ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
11/6/2005 1:15:59 AM

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?

0
lupina
11/6/2005 10:13:31 AM
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.DataSource= ds;
DataGridMain.DataKeyField = "ho_id";
DataGridMain.DataBind();
}

private void DataGridMain_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBoxInfo.Text = DataGridMain.DataKeys[e.Item.ItemIndex].ToString();
DataGridMain.EditItemIndex = -1;
Load();
}


but in my case, Dataset that filled DataGrid doesn't contain ho_id field and I must somehow populate DataKeyField in different way.

0
lupina
11/6/2005 11:56:10 AM
Reply: