Reputation: 179
I have a gridview with template fields consisting of dropdowns and textboxes. 1 dropdown will be a so-called "master" dropdown that when the selected index changes, other dropdowns in the same row will be databinded using the selected value of the master dropdown.
I have this code for my ASPX
<asp:GridView ID="gvEducations" runat="server" AutoGenerateColumns="false"
DataKeyNames="file2_id" onrowcancelingedit="gvEducations_RowCancelingEdit"
onrowdatabound="gvEducations_RowDataBound"
onrowdeleting="gvEducations_RowDeleting" onrowediting="gvEducations_RowEditing"
onrowupdating="gvEducations_RowUpdating">
<Columns>
<asp:BoundField HeaderText="file2_id" DataField="file2_id" ReadOnly="true" Visible="False"/>
<asp:TemplateField HeaderText="Education Establishment">
<ItemTemplate>
<%# Eval("education_establishment_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditEducationEstablishment" runat="server"
OnSelectedIndexChanged="ddlEditEducationEstablishment_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="School">
<ItemTemplate>
<%# Eval("school_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditSchool" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="School Others" Visible="false">
<ItemTemplate>
<%# Eval("school_others") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditSchoolOthers" runat="server" MaxLength="81"
Text='<%# Eval("school_others") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("start_date")).ToString("d")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditStartDate" runat="server" type="date"
Text='<%# Bind("start_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("end_date")).ToString("d")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditEndDate" runat="server" type="date"
Text='<%# Bind("end_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch Of Study 1">
<ItemTemplate>
<%# Eval("branch_of_study_1_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditBranchOfStudy1" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Certificate">
<ItemTemplate>
<%# Eval("certificate_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditCertificate" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Appraisal">
<ItemTemplate>
<%# Eval("course_appraisal") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCourseAppraisal" runat="server" MaxLength="30"
Text='<%# Eval("course_appraisal") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch Of Study 2">
<ItemTemplate>
<%# Eval("branch_of_study_2_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditBranchOfStudy2" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" UseSubmitBehavior="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Update" UseSubmitBehavior="false"/>
<asp:Button ID="btnCanel" runat="server" Text="Cancel" CommandName="Cancel" UseSubmitBehavior="false"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" UseSubmitBehavior="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind (RowDataBound Gridview events)
protected void gvEducations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex)
{
int file2Id = int.Parse(gvEducations.DataKeys[e.Row.RowIndex].Value.ToString());
DataTable dt = new DataTable();
dt = file2BLO.SelectSpecificFile2(file2Id);
DropDownList ddlEditEducationEstablishment =
(DropDownList)e.Row.FindControl("ddlEditEducationEstablishment");
ddlEditEducationEstablishment.DataSource =
educationEstablishmentBLO.SelectAllEducationEstablishment();
ddlEditEducationEstablishment.DataValueField = "education_establishment_code";
ddlEditEducationEstablishment.DataTextField = "education_establishment";
ddlEditEducationEstablishment.DataBind();
ddlEditEducationEstablishment.SelectedValue =
dt.Rows[0]["education_establishment_code"].ToString();
string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool");
ddlEditSchool.DataSource =
schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
ddlEditSchool.DataValueField = "school_code";
ddlEditSchool.DataTextField = "school";
ddlEditSchool.DataBind();
ddlEditSchool.SelectedValue = dt.Rows[0]["school_code"].ToString();
DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl
("ddlEditBranchOfStudy1");
ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO.
SelectBranchOfEstablishment(educationEstablishmentCode);
ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code";
ddlEditBranchOfStudy1.DataTextField = "branch_of_study";
ddlEditBranchOfStudy1.DataBind();
ddlEditBranchOfStudy1.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString();
DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl
("ddlEditCertificate");
ddlEditCertificate.DataSource = certificateBLO.
SelectCertificateOfEstablishment(educationEstablishmentCode);
ddlEditCertificate.DataValueField = "certificate_code";
ddlEditCertificate.DataTextField = "certificate";
ddlEditCertificate.DataBind();
ddlEditCertificate.SelectedValue = dt.Rows[0]["certificate_code"].ToString();
DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row.
FindControl("ddlEditBranchOfStudy2");
ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO.
SelectBranchOfEstablishment(educationEstablishmentCode);
ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code";
ddlEditBranchOfStudy2.DataTextField = "branch_of_study";
ddlEditBranchOfStudy2.DataBind();
ddlEditBranchOfStudy2.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString();
}
}
This is what I have tried:
protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex)
//{
// DropDownList ddlEditEducationEstablishment =
// (DropDownList)e.Row.FindControl("ddlEditEducationEstablishment");
// string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
// DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool");
// ddlEditSchool.DataSource =
// schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
// ddlEditSchool.DataValueField = "school_code";
// ddlEditSchool.DataTextField = "school";
// ddlEditSchool.DataBind();
// DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl
// ("ddlEditBranchOfStudy1");
// ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO.
// SelectBranchOfEstablishment(educationEstablishmentCode);
// ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code";
// ddlEditBranchOfStudy1.DataTextField = "branch_of_study";
// ddlEditBranchOfStudy1.DataBind();
// DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl
// ("ddlEditCertificate");
// ddlEditCertificate.DataSource = certificateBLO.
// SelectCertificateOfEstablishment(educationEstablishmentCode);
// ddlEditCertificate.DataValueField = "certificate_code";
// ddlEditCertificate.DataTextField = "certificate";
// ddlEditCertificate.DataBind();
// DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row.
// FindControl("ddlEditBranchOfStudy2");
// ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO.
// SelectBranchOfEstablishment(educationEstablishmentCode);
// ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code";
// ddlEditBranchOfStudy2.DataTextField = "branch_of_study";
// ddlEditBranchOfStudy2.DataBind();
//}
}
What I want is that when I select in the first dropdown, the other dropdowns will be databinded again using the selected value in the first dropdown. Refer to the image below.
Upvotes: 2
Views: 8182
Reputation: 3949
Like I said in my comment, you need to find the GridViewRow that your DropDownList is contained within. With that row, you can then find all your other DropDownLists. You are on the right track by finding each of the other DropDownLists, but you aren't looking for them in the correct location.
protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the master DropDownList and its value
DropDownList ddlEditEducationEstablishment = (DropDownList)sender;
string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
// Get the GridViewRow in which this master DropDownList exists
GridViewRow row = (GridViewRow)ddlEditEducationEstablishment.NamingContainer;
// Find all of the other DropDownLists within the same row and bind them
DropDownList ddlEditSchool = (DropDownList)row.FindControl("ddlEditSchool");
ddlEditSchool.DataSource = schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
ddlEditSchool.DataValueField = "school_code";
ddlEditSchool.DataTextField = "school";
ddlEditSchool.DataBind();
// etc...
}
Upvotes: 3