Dynamically bind dropdown list etc in template column

  • Thread starter Thread starter Dmitry Korolyov [MVP]
  • Start date Start date
D

Dmitry Korolyov [MVP]

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 said:
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)./

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.
 
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 [[email protected]]
MVP: Windows Server - Active Directory


Craig Deelsnyder said:
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)./

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.
 
Dmitry said:
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 [[email protected]]
MVP: Windows Server - Active Directory



"Craig Deelsnyder" <[email protected]
Dmitry said:
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)./

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.

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....
 
Back
Top