By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Dynamically bind dropdown list etc in template column

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.