I want to filter grid with 2 Drop downs which are outside the gridview scope. First one is Class Dropdown & Second One is Exam DropDown.
I want to get filtered record after selecting class and then exam so that i can get the filtered record of exam schedule of that class.
My BackEnd Code is:
protected void BindExamdropdown()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Exam", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
ddlexamid.DataSource = dt;
ddlexamid.DataTextField = "ExamName";
ddlexamid.DataValueField = "ExamID";
ddlexamid.DataBind();
ddlexamid.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void BindClassdropdown()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Class", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
ddlclassid.DataSource = dt;
ddlclassid.DataTextField = "StudentClassName";
ddlclassid.DataValueField = "StudentClassID";
ddlclassid.DataBind();
ddlclassid.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void ddlclassid_SelectedIndexChanged(object sender, EventArgs e)
{
string StudentClassName = Convert.ToString(ddlclassid.SelectedItem);
string ExamName = Convert.ToString(ddlexamid.SelectedItem);
SqlConnection oconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
oconn.Open();
SqlCommand ocmd = new SqlCommand("Select SubjectExamSchedule.SubjectExamID,Section.StudentSectionName,Class.StudentClassName,Subject.SubjectName,Exam.ExamName,SubjectExamSchedule.ExamDate from Student Inner join Section On Section.StudentSectionID = Student.StudentSectionID Inner join Class On Class.StudentClassID = Section.StudentClassID Inner join Subject On Subject.StudentClassID = Class.StudentClassID Inner Join SubjectExamSchedule On SubjectExamSchedule.SubjectID = Subject.SubjectID Inner Join Exam On Exam.ExamID = SubjectExamSchedule.ExamID where Class.StudentClassName='" + StudentClassName + "'", oconn);
SqlDataAdapter oda = new SqlDataAdapter(ocmd);
SqlCommandBuilder builder = new SqlCommandBuilder(oda);
DataSet ds = new DataSet();
oda.Fill(ds);
oconn.Close();
gvexamscheduleshow.DataSource = ds;
gvexamscheduleshow.DataBind();
}
protected void ddlexamid_SelectedIndexChanged(object sender, EventArgs e)
{
string ExamName = Convert.ToString(ddlexamid.SelectedItem);
SqlConnection oconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
oconn.Open();
SqlCommand ocmd = new SqlCommand("Select SubjectExamSchedule.SubjectExamID,Section.StudentSectionName,Class.StudentClassName,Subject.SubjectName,Exam.ExamName,SubjectExamSchedule.ExamDate from Student Inner join Section On Section.StudentSectionID = Student.StudentSectionID Inner join Class On Class.StudentClassID = Section.StudentClassID Inner join Subject On Subject.StudentClassID = Class.StudentClassID Inner Join SubjectExamSchedule On SubjectExamSchedule.SubjectID = Subject.SubjectID Inner Join Exam On Exam.ExamID = SubjectExamSchedule.ExamID where Class.StudentClassName='" + ExamName + "'", oconn);
SqlDataAdapter oda = new SqlDataAdapter(ocmd);
SqlCommandBuilder builder = new SqlCommandBuilder(oda);
DataSet ds = new DataSet();
oda.Fill(ds);
oconn.Close();
gvexamscheduleshow.DataSource = ds;
gvexamscheduleshow.DataBind();
}
My Html Code is:
<asp:DropDownList ID="ddlclassid" runat="server" class='form-control' OnSelectedIndexChanged="ddlclassid_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="ddlexamid" runat="server" class='form-control' OnSelectedIndexChanged="ddlclassid_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>