Inserire una DropDownlList in un DataGrid
<%@ Page language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<HTML>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
LoadTable();
}
private void LoadTable()
{
OleDbConnection dbConn;
OleDbDataAdapter myAdapter;
string sqlCmd;
string strConn;
DataSet oDataSet = new DataSet();
strConn =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("database/prodotti.mdb") + ";" +
"Persist Security Info=False";
sqlCmd =
" SELECT" +
" prodotti.codice," +
" prodotti.nome," +
" prodotti.descrizione," +
" prodotti.categoria as codicecategoria," +
" categorie.categoria" +
" FROM categorie" +
" INNER JOIN prodotti" +
" ON categorie.codice = prodotti.categoria";
dbConn = new OleDbConnection(strConn);
myAdapter = new OleDbDataAdapter(sqlCmd, dbConn);
myAdapter.Fill(oDataSet, "prodotti");
myDatagrid.DataSource = oDataSet.Tables["prodotti"].DefaultView;
myDatagrid.DataBind();
}
private void LoadCategorie(DropDownList ddl, string selected)
{
OleDbConnection dbConn;
string sqlCmd;
string strConn;
strConn =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("database/prodotti.mdb") + ";" +
"Persist Security Info=False";
sqlCmd =
" SELECT" +
" categorie.codice as codicecategoria," +
" categorie.categoria" +
" FROM categorie";
dbConn = new OleDbConnection(strConn);
dbConn.Open();
OleDbCommand dbComm = new OleDbCommand(sqlCmd, dbConn);
OleDbDataReader aReader = dbComm.ExecuteReader();
try
{
ddl.Items.Clear();
while (aReader.Read())
{
ListItem li = new ListItem(
aReader["categoria"].ToString(),
aReader["codicecategoria"].ToString() );
if (selected==aReader["codicecategoria"].ToString())
li.Selected = true;
ddl.Items.Add(li);
}
}
finally
{
aReader.Close();
dbConn.Close();
}
}
private void aggiornaProdotto(string codice, string nome, string descrizione, string categoria)
{
OleDbConnection dbConn;
string sqlCmd;
string strConn;
strConn =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("database/prodotti.mdb") + ";" +
"Persist Security Info=False";
sqlCmd =
" update prodotti " +
" set nome = '" + nome + "'," +
" descrizione = '" + descrizione + "'," +
" categoria = " + categoria +
" WHERE codice = " + codice;
dbConn = new OleDbConnection(strConn);
dbConn.Open();
OleDbCommand dbComm = new OleDbCommand(sqlCmd, dbConn);
try
{
dbComm.ExecuteScalar();
}
finally
{
dbConn.Close();
}
}
private void myDatagrid_OnEditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDatagrid.EditItemIndex = e.Item.ItemIndex;
LoadTable();
}
private void myDatagrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDatagrid.EditItemIndex = -1;
LoadTable();
}
private void myDatagrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
//leggo i valori di default
string codiceCategoria = e.Item.Cells[5].Text;
DropDownList ddl = null;
ddl = (DropDownList)e.Item.FindControl("ddlCategorie");
//carico la DropdownList
LoadCategorie(ddl, codiceCategoria);
}
}
private void myDatagrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string codice = e.Item.Cells[0].Text;
string nome = ((TextBox)(e.Item.FindControl("tbDGNome"))).Text;
string descrizione = ((TextBox)(e.Item.FindControl("tbDGDescrizione"))).Text;
string categoria = ((DropDownList)(e.Item.FindControl("ddlCategorie"))).SelectedValue;
aggiornaProdotto(codice, nome, descrizione, categoria);
myDatagrid.EditItemIndex = -1;
LoadTable();
}
</script>
<body>
<form runat="server">
<asp:datagrid id="myDatagrid" runat="server" AutoGenerateColumns="False" OnEditCommand="myDatagrid_OnEditCommand"
OnCancelCommand="myDatagrid_CancelCommand" OnItemDataBound="myDatagrid_ItemDataBound" OnUpdateCommand="myDatagrid_UpdateCommand">
<Columns>
<asp:BoundColumn DataField="codice" ReadOnly="True" HeaderText="codice"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="nome">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.nome") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=tbDGNome runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.nome") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="descrizione">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.descrizione") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=tbDGDescrizione runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.descrizione") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="categoria">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.categoria") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCategorie" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="codicecategoria" ReadOnly="True" HeaderText="codicecategoria"></asp:BoundColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>
Scarica il Codice...
Stampa la pagina