Mi codice :
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" EmptyDataText="Il n'y a aucun enregistrement de données à afficher." OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" Visible="False" />
<asp:BoundField DataField="userid" HeaderText="userid" SortExpression="userid" Visible="False" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:TemplateField HeaderText="projet" SortExpression="projet">
<EditItemTemplate>
<asp:DropDownList ID="ddlprojet" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlproj_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("projet") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="activite" SortExpression="activite">
<EditItemTemplate>
<asp:DropDownList ID="ddlactivite" runat="server" AutoPostBack="True">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("activite") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="duree" HeaderText="duree" SortExpression="duree" />
<asp:BoundField DataField="commentaire" HeaderText="commentaire" SortExpression="commentaire" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ldapConnectionString1 %>"
DeleteCommand="DELETE FROM [cra] WHERE [id] = @id" InsertCommand="INSERT INTO [cra] ([userid], [date], [projet], [activite], [duree], [commentaire]) VALUES (@userid, @date, @projet, @activite, @duree, @commentaire)"
ProviderName="<%$ ConnectionStrings:ldapConnectionString1.ProviderName %>" SelectCommand="SELECT [id], [userid], [date], [projet], [activite], [duree], [commentaire] FROM [cra]"
UpdateCommand="UPDATE [cra] SET [userid] = @userid, [date] = @date, [projet] = @projet, [activite] = @activite, [duree] = @duree, [commentaire] = @commentaire WHERE [id] = @id">
<InsertParameters>
<asp:Parameter Name="userid" Type="String" />
<asp:Parameter Name="date" Type="String" />
<asp:Parameter Name="projet" Type="String" />
<asp:Parameter Name="activite" Type="String" />
<asp:Parameter Name="duree" Type="Int32" />
<asp:Parameter Name="commentaire" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="userid" Type="String" />
<asp:Parameter Name="date" Type="String" />
<asp:Parameter Name="projet" Type="String" />
<asp:Parameter Name="activite" Type="String" />
<asp:Parameter Name="duree" Type="Int32" />
<asp:Parameter Name="commentaire" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DSProjet" runat="server" ConnectionString="<%$ ConnectionStrings:ldapConnectionString1 %>" SelectCommand="SELECT DISTINCT [projet] FROM [proact]"></asp:SqlDataSource>
<asp:SqlDataSource ID="DSactivite" runat="server" ConnectionString="<%$ ConnectionStrings:ldapConnectionString1 %>" SelectCommand="SELECT [activite] FROM [proact] WHERE ([projet] = @projet)">
<SelectParameters>
<asp:Parameter Name="projet" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
Code behind :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlprojet = (DropDownList)e.Row.FindControl("ddlprojet");
ddlprojet.DataSource = DSProjet;
ddlprojet.DataTextField = "projet";
ddlprojet.DataValueField = "projet";
ddlprojet.DataBind();
//Seleziono lo Cser appropriato
string nomprojet = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "projet"));
ListItem li = ddlprojet.Items.FindByValue(nomprojet);
if (li != null)
{
li.Selected = true;
}
// Popolo la DDL dei Cart in base allo Cser selezionato
string nomactivite = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "activite"));
DropDownList ddlactivite = (DropDownList)e.Row.FindControl("ddlactivite");
PopolaCart(ddlactivite, ddlprojet, nomactivite, nomprojet);
if (nomprojet == "")
{
nomprojet = ddlprojet.SelectedItem.Value;
nomactivite = ddlactivite.SelectedItem.Value;
}
}
}
protected void ddlproj_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlprojet = (DropDownList)sender;
GridViewRow gvr = (GridViewRow)ddlprojet.Parent.Parent;
DropDownList ddlactivite = (DropDownList)gvr.FindControl("ddlprojet");
PopolaCart(ddlactivite, ddlprojet, "", "");
string nomprojet = ddlprojet.SelectedItem.Value;
string nomactivite = ddlactivite.SelectedItem.Value;
}
private void PopolaCart(DropDownList ddlactivite, DropDownList ddlprojet, string nomactivite, string nomprojet)
{
DSactivite.SelectParameters["???"].DefaultValue = ddlprojet.SelectedValue;
ddlactivite.DataSource = DSactivite;
ddlactivite.DataTextField = "activite";
ddlactivite.DataValueField = "activite";
ddlactivite.DataBind();
// ora seleziono l'appropriato Cart
ListItem li2 = ddlactivite.Items.FindByValue(nomactivite);
if (li2 != null)
{
li2.Selected = true;
}
}
Modificato da cnguyen il 04 giugno 2006 13.18 -