473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamically setting datagrid column in code-behind?

Roy
Hey all,

On my html page I have a datagrid with the column:
<boundColumn datafield="xyz" visible = false>
</boundColumn>

In my code behind, within item data bound event, I dynamically set the
column text like so:
e.item.cells(10).text = mystring.trim

Why I'm doing this is because I'm essentially using the invisible
boundcolumn to store info for use by this datagrid column's hyperlink:

<templatecolumn>
<itemtemplate>
<hyperlink navigateurl="dnl.aspx?a=xyz"

e.item.cells(10) is being set properly and contains the data (or as
best as I can tell). However, the templatecolumn's hyperlink never
changes and always displays the default data in the link.
It's almost as if the hyperlink info gets set prior to the boundcolumn.
How can I get around this?

Thanks.

Nov 19 '05 #1
9 6427
You can use the OnItemDataBound event.

if you set an ID for you HyperLink (or use its ordinal position), you can
modify from the code behind.

e.g.

private void DataGrid1_OnItemDataBound(object Sender,
System.Data.DataItemEventArgs e)
{
if(e.Item.ItemTemplate != ListItemType.Header && e.Item.ItemTemplate !=
ListItemType.Footer)
{
((Hyperlink)e.Item.FindControl("Hyperlink1")).Navi gateUrl =
"dnl.aspx?a=" + [value goes here]; //This could be e.Item.Cells[0].Text for
example
}
}

hope that helps

"Roy" <ro**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hey all,

On my html page I have a datagrid with the column:
<boundColumn datafield="xyz" visible = false>
</boundColumn>

In my code behind, within item data bound event, I dynamically set the
column text like so:
e.item.cells(10).text = mystring.trim

Why I'm doing this is because I'm essentially using the invisible
boundcolumn to store info for use by this datagrid column's hyperlink:

<templatecolumn>
<itemtemplate>
<hyperlink navigateurl="dnl.aspx?a=xyz"

e.item.cells(10) is being set properly and contains the data (or as
best as I can tell). However, the templatecolumn's hyperlink never
changes and always displays the default data in the link.
It's almost as if the hyperlink info gets set prior to the boundcolumn.
How can I get around this?

Thanks.

Nov 19 '05 #2
If all you need is just to use the value of another field in the datarecord
without displaying it, you don't need to allocate a datagrid column for
that. Use property DataItem of the datagrid item in ItemDataBound event.
Typecast it to you record type and get the field value from there.

Eliyahu

"Roy" <ro**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hey all,

On my html page I have a datagrid with the column:
<boundColumn datafield="xyz" visible = false>
</boundColumn>

In my code behind, within item data bound event, I dynamically set the
column text like so:
e.item.cells(10).text = mystring.trim

Why I'm doing this is because I'm essentially using the invisible
boundcolumn to store info for use by this datagrid column's hyperlink:

<templatecolumn>
<itemtemplate>
<hyperlink navigateurl="dnl.aspx?a=xyz"

e.item.cells(10) is being set properly and contains the data (or as
best as I can tell). However, the templatecolumn's hyperlink never
changes and always displays the default data in the link.
It's almost as if the hyperlink info gets set prior to the boundcolumn.
How can I get around this?

Thanks.

Nov 19 '05 #3
Hi Roy,

The simple way is to use HyperLinkColumn:

<asp:HyperLinkColumn DataTextField="xyz" DataNavigateUrlField="xyz"
DataNavigateUrlFormatString="dnl.aspx?a={0}" ></asp:HyperLinkColumn>

HTH

Elton
el********@hotmail.com
"Roy" wrote:
Hey all,

On my html page I have a datagrid with the column:
<boundColumn datafield="xyz" visible = false>
</boundColumn>

In my code behind, within item data bound event, I dynamically set the
column text like so:
e.item.cells(10).text = mystring.trim

Why I'm doing this is because I'm essentially using the invisible
boundcolumn to store info for use by this datagrid column's hyperlink:

<templatecolumn>
<itemtemplate>
<hyperlink navigateurl="dnl.aspx?a=xyz"

e.item.cells(10) is being set properly and contains the data (or as
best as I can tell). However, the templatecolumn's hyperlink never
changes and always displays the default data in the link.
It's almost as if the hyperlink info gets set prior to the boundcolumn.
How can I get around this?

Thanks.

Nov 19 '05 #4
Roy
Thanks for the tip Grant, I'll give it a whirl and get back to you.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #5
Roy
Thanks for the post Eliyahu, I only wish it were so easy. :)

The field value is external to the datagrid.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #6
Roy
Thanks for the post Elton, but the hyperlinkcolumn can only pass single
parameters. I need to pass 5, hence, the templatecolumn.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #7
I am afraid you missed the point. DataItem property refers to the data
record used for populating the grid. The record is not a part of the grid.
It is what the grid gets column values from.

Eliyahu

"Roy" <SP*******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks for the post Eliyahu, I only wish it were so easy. :)

The field value is external to the datagrid.
*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #8
In datagrid_ItemDataBound event, you can also rebiuld hyperlinkcolumn's url
and pass 5 parameters to it.

HTH

Elton

"Roy" wrote:
Thanks for the post Elton, but the hyperlinkcolumn can only pass single
parameters. I need to pass 5, hence, the templatecolumn.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #9
In datagrid_ItemDataBound event, you can also rebiuld hyperlinkcolumn's url
and pass 5 parameters to it.

HTH

Elton

"Roy" wrote:
Thanks for the post Elton, but the hyperlinkcolumn can only pass single
parameters. I need to pass 5, hence, the templatecolumn.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
2
by: Colin McGuigan | last post by:
This is a translation of an ASP page to ASP.Net. First, the background: The goal is to have a grid of different settings for the application -- think something along the lines of the Property...
1
by: GP | last post by:
I have created a datagrid that is creating columns dynamically at runtime using the template columns.Edit ,add & delete works fine. The only problem I face is with sorting,I am setting the sorting...
5
by: Dennis | last post by:
I have a class that inherits from DataGrid. I can set the rowheights in a DataGrid by tappig into the "get_Datagridrows" method. However, this does not work for classes that inherit from...
4
by: Rich | last post by:
Hello, I have a one datagrid that will be based on different datatables. One datatable may have 7 columns, another 15... With the tables that have more columns, I have been manually dragging...
0
by: herman404 | last post by:
Hi everyone, I'm trying to dynamically add 3 button columns to a DataGrid object that I have on an ASP.net webpage, but I don't see any documentation on how to do it. I've seen plenty of examples...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
0
by: Glenn Rosenthal | last post by:
I have a Web page with a datagrid for display search results. The first column contains a check box and a hidden field, and are defined with the datagrid. <asp:DataGrid...
0
by: lucindaa | last post by:
Hi EveryBody, i have a problem in setting the column type of datagrid dynamically in C# .Net Windows App. i am generating the grid automatically by assigning datasource of a datatable the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.