469,292 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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 3585
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by HalaszJ | last post: by
1 post views Thread by Deepa | last post: by
7 posts views Thread by Jed | last post: by
4 posts views Thread by Craig Buchanan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.