473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="dn l.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 6439
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_OnIte mDataBound(obje ct Sender,
System.Data.Dat aItemEventArgs e)
{
if(e.Item.ItemT emplate != ListItemType.He ader && e.Item.ItemTemp late !=
ListItemType.Fo oter)
{
((Hyperlink)e.I tem.FindControl ("Hyperlink1")) .NavigateUrl =
"dnl.aspx?a =" + [value goes here]; //This could be e.Item.Cells[0].Text for
example
}
}

hope that helps

"Roy" <ro**********@g mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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="dn l.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**********@g mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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="dn l.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:HyperLinkC olumn DataTextField=" xyz" DataNavigateUrl Field="xyz"
DataNavigateUrl FormatString="d nl.aspx?a={0}" ></asp:HyperLinkCo lumn>

HTH

Elton
el********@hotm ail.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="dn l.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*******@gmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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_ItemDa taBound 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_ItemDa taBound 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
2431
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 DataGrid in a web user control (.ascx) I am creating. This applies to VS.NET 2003 / Framework 1.1.
2
2024
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 Page for a control. You have two columns -- the first column is a simple caption, and the second column can be one of a number of controls (textbox,...
1
1938
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 expression to the column name and checked the allow sorting checkbox to true. The code never reaches the DataGrid1_SortCommand,But I do see the...
5
4731
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 DataGrid. Anyone know how to do this on derived classes? I find it apalling that Microsoft provided a DataGrid that is so inflexible! -- Dennis in...
4
4184
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 column widths to narrow them. I want to record these widths so I can reapply them when I bind the datagrid to the table with 15 columns. I have...
0
2021
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 on how to add regular columns, but how do you declare a button column? My code is posted below, I have a placeholder on a webform, and the datagrid...
3
4350
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 condition1 is true, then the SqlDataReader will be populated with the records existing in table1 & will return 0 to the calling function but if...
4
10996
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 to iterate thru the gridview's rows, examine the databound controls, then perform a database action. for some reason, i can't find the controls. ...
0
1400
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 runat="server" ID="dgPaused" AutoGenerateColumns="false" AllowPaging="false" AllowSorting="true" cellspacing="0" cellpadding="3" rules="all" border="1"...
0
1730
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 columns are dynamicallygenerated when i do like this all the columns are generated as textbox but i want some of the column should be in combo box,...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.