473,387 Members | 1,516 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,387 software developers and data experts.

Dynamically bind dropdown list etc in template column

Is that possible? In other words, I want a dropdown list (and other list-type controls) which appears in edit more of a templated column to be populated with data at the run time. An attempt to do so results in non-existing object error. (Object reference not set to an instance of an object).

--
Dmitry Korolyov [d_**@removethispart.mail.ru]
MVP: Windows Server - Active Directory
Nov 17 '05 #1
3 3769
Dmitry Korolyov [MVP] wrote:
Is that possible? In other words, I want a dropdown list (and other
list-type controls) which appears in edit more of a templated column to
be populated with data at the run time. An attempt to do so results in
non-existing object error. (/Object reference not set to an instance of
an object)./

--
Dmitry Korolyov [d_**@removethispart.mail.ru]
MVP: Windows Server - Active Directory


Check out the ItemDataBound event. This runs once for each item in the
grid (or datalist) (in other words each row). e.Item.DataItem will give
you the corresponding data item the current row was bound to, and you
can populate your dropdown as needed there.

Now to get reference to a control, as an example say we're trying to
find a label in the first column (index 0), you would do the following
in ItemDataBound

protected void myDataGrid_ItemDataBound(Object sender,
DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
//bind/populate the label
Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");

//note i'm assuming i bound the grid to a dataview
labelField.Text =
((DataRowView)e.Item.DataItem)["col_text"].ToString();
}
}

Hopefully I didn't put any syntax errors in there as I wrote this
without my IDE, but there's also various examples on sites such as
4guysfromrolla.com and others.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 17 '05 #2
Thanks, but the example is populating the control from the datasource bound to the datagrid in general, while I need to populate it from some "abstract" data source - created at runtime or created at application level, something like this.

--
Dmitry Korolyov [d_**@removethispart.mail.ru]
MVP: Windows Server - Active Directory
"Craig Deelsnyder" <cd******@NOSPAMyahoo.com> wrote in message news:e8**************@TK2MSFTNGP09.phx.gbl...
Dmitry Korolyov [MVP] wrote:
Is that possible? In other words, I want a dropdown list (and other
list-type controls) which appears in edit more of a templated column to
be populated with data at the run time. An attempt to do so results in
non-existing object error. (/Object reference not set to an instance of
an object)./

--
Dmitry Korolyov [d_**@removethispart.mail.ru]
MVP: Windows Server - Active Directory



Check out the ItemDataBound event. This runs once for each item in the
grid (or datalist) (in other words each row). e.Item.DataItem will give
you the corresponding data item the current row was bound to, and you
can populate your dropdown as needed there.

Now to get reference to a control, as an example say we're trying to
find a label in the first column (index 0), you would do the following
in ItemDataBound

protected void myDataGrid_ItemDataBound(Object sender,
DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
//bind/populate the label
Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");

//note i'm assuming i bound the grid to a dataview
labelField.Text =
((DataRowView)e.Item.DataItem)["col_text"].ToString();
}
}

Hopefully I didn't put any syntax errors in there as I wrote this
without my IDE, but there's also various examples on sites such as
4guysfromrolla.com and others.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 17 '05 #3
Dmitry Korolyov [MVP] wrote:
Thanks, but the example is populating the control from the datasource
bound to the datagrid in general, while I need to populate it from some
"abstract" data source - created at runtime or created at application
level, something like this.

--
Dmitry Korolyov [d_**@removethispart.mail.ru]
MVP: Windows Server - Active Directory

"Craig Deelsnyder" <cd******@NOSPAMyahoo.com
<mailto:cd******@NOSPAMyahoo.com>> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Dmitry Korolyov [MVP] wrote:
> Is that possible? In other words, I want a dropdown list (and other
> list-type controls) which appears in edit more of a templated

column to
> be populated with data at the run time. An attempt to do so

results in
> non-existing object error. (/Object reference not set to an

instance of
> an object)./
>
> --
> Dmitry Korolyov [d_**@removethispart.mail.ru]
> MVP: Windows Server - Active Directory
>
>


Check out the ItemDataBound event. This runs once for each item in the
grid (or datalist) (in other words each row). e.Item.DataItem will
give
you the corresponding data item the current row was bound to, and you
can populate your dropdown as needed there.

Now to get reference to a control, as an example say we're trying to
find a label in the first column (index 0), you would do the following
in ItemDataBound

protected void myDataGrid_ItemDataBound(Object sender,
DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
//bind/populate the label
Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");

//note i'm assuming i bound the grid to a dataview
labelField.Text =
((DataRowView)e.Item.DataItem)["col_text"].ToString();
}
}

Hopefully I didn't put any syntax errors in there as I wrote this
without my IDE, but there's also various examples on sites such as
4guysfromrolla.com and others.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


You can bind it to whatever you want, I just showed that as an example,
since I assume you might need some value in the current row to use to
get the data to bind the ddl with...if not, feel free to bind it as you
normally would....

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 17 '05 #4

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

Similar topics

1
by: HalaszJ | last post by:
Is there no work around for this? >-----Original Message----- >I have asked this question on about every board I can >think of and I have yet to get an answer, so If anyone >would know I...
1
by: Deepa | last post by:
I am doing an ASP.Net application wherein I have a datagrid which I need to populate dynamically. The grid has 5 rows and two columns. In the 2nd col I need to insert dropdown boxes. I'll have to...
7
by: Jed | last post by:
I have a web service method that returns an array of custom objects marked serializeable with fully described public properties. When I bind the results to a DataGrid I can access the properties...
6
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I...
5
by: Alan Silver | last post by:
Hello, I have a products page that takes a product ID in the query string. Based on the product details (from a database), the page then loads up one of a number of custom controls, calls a...
3
by: Advertis | last post by:
Is there a way to populate a dropdown in a GridView with the next x years? I am using a GridView to display data from a SQL table One of the fields is a Year. I want to have the dropdown list not...
1
by: spitapps | last post by:
I have a gridview declaratively added to my webform. This gridview displays data from a stored procedure returned as a dataset. Once the dataset has been modified slightly i bind it to my...
0
by: GMartin | last post by:
I have a pop-up form with a three columned Grid that has checkboxes in a Template Column in the first/left-most column. (The form is to allow users to select "Members" of a group, where they check...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.