472,371 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 3696
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.