|
| GridView containing a Custom Databound dropdown |
 |
Mon, 17 Mar 2008 15:59:37 +000 |
I wanted to use a gridview that contained a dropdown for users to select a value
from a list. The list should only require the insertion of a row in an SQL
table and no code changes.
Here is a simple way to perform this - it really uses the minumum of code
------- The Custom Drop Downnamespace YourNameSpace
{
publicclassDropDownListStatus : DropDownList
{publicoverridevoid DataBind()
{
// load the data for tis drop down and bind the control - only do this during
the databind event, and not INIT or LOAD of the control, otherwise you may run
into issues which I wont go into here
LoadData();base.DataBind();
}void LoadData()
{
// you need to create a data source for your data - in my case, a dataset in the
project was sufficientDataSetStatusTableAdapters.TaStatus ta = new
DataSetStatusTableAdapters.TaStatus();
DataSetStatus.StatusDataTable dt = ta.GetData();base.DataSource = dt;
base.DataValueField = "StatusId";base.DataTextField =
"StatusLevel";
}publicint SelectedValueInt
{
get
{int selectedValue = 0;
int.TryParse(this.SelectedValue, out selectedValue);return selectedValue;
}
set
{this.SelectedValue = value.ToString();
}
}
}
}
WIthin the columns of your gridview, you add the column template based on your
custom control registration.
<%@RegisterNamespace="YourNameSpace"TagPrefix="pix"
%>
When selecting your data, select both the ID and text used in the drop down.
During viewing of the data, show the text, and during editing, bind to the
integer value that represents it. This pair is usually in a separate table,
linked by foriegn key to the integer
===============
<asp:TemplateFieldHeaderText="Status"SortExpression="StatusLev
el">
<ItemTemplate><%# Eval("StatusLevel") %>
</ItemTemplate>
<EditItemTemplate>
<pix:DropDownListStatusrunat="server"ID="dds1"SelectedVal
ueInt='<%# Bind("StatusId") %>'/>
</EditItemTemplate>
</asp:TemplateField>
|
| Post Reply
|
|
|
|
|
|
|
|
|
|