通过下拉菜单筛选GridView后编辑不起作用

时间:2016-08-01 17:50:30

标签: c# asp.net gridview

在过滤GridView之后,我被困在编辑编辑点下拉列表(即下拉列表放在gridview之外)。 当我在过滤gridview后单击editlink时,它会使整个网格的起始行可编辑。 我想在ASP.NET而不是J-Query或Ajax中执行此操作。 任何人,请帮我这个??? 这是我的代码:`

        protected void FillGrid()
    {
        //String StudentSectionID = Convert.ToString(ddlsectionid.SelectedValue);
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
        con.Open();
        string query = "  SELECT St.StudentID, StudentNo, StudentPassword, StudentName, Cl.StudentClassName, Cl.StudentClassID,Se.StudentSectionID,Ge.StudentGenderID,Gu.StudentGuardianID,Tr.StudentTransportID,Se.StudentSectionName, StudentRollno, StudentSession, Ge.StudentGenderName, StudentPhone,StudentAddress, StudentEmail, Gu.StudentGuardianName, StudentReligion,Tr.StudentTransportRouteNo, StudentDOB,StudentFees from Student St Inner join Class Cl ON St.StudentClassID = Cl.StudentClassID Inner Join Section Se ON St.StudentSectionID = Se.StudentSectionID Inner Join Gender Ge ON Ge.StudentGenderID = St.StudentGenderID Inner Join Guardian Gu ON Gu.StudentGuardianID = St.StudentGuardianID Inner Join Transport Tr ON Tr.StudentTransportID = St.StudentTransportID";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        gv_student.DataSource = ds;
        gv_student.DataBind();
    }
protected void ddlsectionid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string StudentSectionName = Convert.ToString(ddlsectionid.SelectedItem);
        SqlConnection oconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
        oconn.Open();
        SqlCommand ocmd = new SqlCommand("SELECT St.StudentID,StudentNo,StudentPassword, StudentName, Cl.StudentClassName, Se.StudentSectionName,Se.StudentSectionID, StudentRollno, StudentSession, Ge.StudentGenderName, StudentPhone, StudentAddress, StudentEmail, Gu.StudentGuardianName, StudentReligion, Tr.StudentTransportRouteNo, StudentDOB,StudentFees from Student St Inner join Class Cl ON St.StudentClassID = Cl.StudentClassID Inner Join Section Se ON St.StudentSectionID = Se.StudentSectionID Inner Join Gender Ge ON Ge.StudentGenderID = St.StudentGenderID Inner Join Guardian Gu ON Gu.StudentGuardianID = St.StudentGuardianID Inner Join Transport Tr ON Tr.StudentTransportID = St.StudentTransportID where Se.StudentSectionName='"+StudentSectionName+"'", oconn);
        SqlDataAdapter oda = new SqlDataAdapter(ocmd);
        SqlCommandBuilder builder = new SqlCommandBuilder(oda);
        DataSet ds = new DataSet();
        oda.Fill(ds);
        gv_student.DataSource = ds;
        gv_student.DataBind();
    }
    protected void gv_student_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlclassid = (DropDownList)e.Row.FindControl("ddlclassid");
                DataTable dt;
                string SQL = "SELECT * FROM Class";

                string sConstr = ConfigurationManager.ConnectionStrings["Final Year Project"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr))
                {
                    using (SqlCommand comm = new SqlCommand(SQL, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt = new DataTable("tbl");
                            da.Fill(dt);
                        }
                    }
                }
                ddlclassid.DataSource = dt;
                ddlclassid.DataTextField = "StudentClassName";
                ddlclassid.DataValueField = "StudentClassID";
                ddlclassid.DataBind();
                ddlclassid.SelectedValue = ((DataRowView)e.Row.DataItem)["StudentClassID"].ToString();
            }
        }

        // For Section
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                DropDownList ddlsectionid = (DropDownList)e.Row.FindControl("ddlsectionid");
                DataTable dt1;
                string SQL1 = "SELECT * FROM Section";

                string sConstr1 = ConfigurationManager.ConnectionStrings["Final Year Project"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr1))
                {
                    using (SqlCommand comm = new SqlCommand(SQL1, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt1 = new DataTable("tbl");
                            da.Fill(dt1);
                        }
                    }
                }
                ddlsectionid.DataSource = dt1;
                ddlsectionid.DataTextField = "StudentSectionName";
                ddlsectionid.DataValueField = "StudentSectionID";
                ddlsectionid.DataBind();
                ddlsectionid.SelectedValue = ((DataRowView)e.Row.DataItem)["StudentSectionID"].ToString();
            }
        }

        //For Gender
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                DropDownList ddlgenderid = (DropDownList)e.Row.FindControl("ddlgenderid");
                DataTable dt2;
                string SQL2 = "SELECT * FROM Gender";

                string sConstr1 = ConfigurationManager.ConnectionStrings["Final Year Project"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr1))
                {
                    using (SqlCommand comm = new SqlCommand(SQL2, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt2 = new DataTable("tbl");
                            da.Fill(dt2);
                        }
                    }
                }
                ddlgenderid.DataSource = dt2;
                ddlgenderid.DataTextField = "StudentGenderName";
                ddlgenderid.DataValueField = "StudentGenderID";
                ddlgenderid.DataBind();
                ddlgenderid.SelectedValue = ((DataRowView)e.Row.DataItem)["StudentGenderID"].ToString();
            }
        }

        //For Guardian
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                DropDownList ddlguardianid = (DropDownList)e.Row.FindControl("ddlguardianid");
                DataTable dt3;
                string SQL3 = "SELECT * FROM Guardian";

                string sConstr1 = ConfigurationManager.ConnectionStrings["Final Year Project"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr1))
                {
                    using (SqlCommand comm = new SqlCommand(SQL3, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt3 = new DataTable("tbl");
                            da.Fill(dt3);
                        }
                    }
                }
                ddlguardianid.DataSource = dt3;
                ddlguardianid.DataTextField = "StudentGuardianName";
                ddlguardianid.DataValueField = "StudentGuardianID";
                ddlguardianid.DataBind();
                ddlguardianid.SelectedValue = ((DataRowView)e.Row.DataItem)["StudentGuardianID"].ToString();
            }
        }

        //For Transport
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                DropDownList ddltransportid = (DropDownList)e.Row.FindControl("ddltransportid");
                DataTable dt4;
                string SQL4 = "SELECT * FROM Transport";

                string sConstr1 = ConfigurationManager.ConnectionStrings["Final Year Project"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr1))
                {
                    using (SqlCommand comm = new SqlCommand(SQL4, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt4 = new DataTable("tbl");
                            da.Fill(dt4);
                        }
                    }
                }
                ddltransportid.DataSource = dt4;
                ddltransportid.DataTextField = "StudentTransportRouteNo";
                ddltransportid.DataValueField = "StudentTransportID";
                ddltransportid.DataBind();
                ddltransportid.SelectedValue = ((DataRowView)e.Row.DataItem)["StudentTransportID"].ToString();
            }
        }

    }
    protected void gv_student_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int StudentID = Convert.ToInt32(gv_student.DataKeys[e.RowIndex].Value);
        SqlConnection oconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
        oconn.Open();
        SqlCommand ocmd = new SqlCommand();
        ocmd.CommandText = "DELETE FROM Student WHERE StudentID=@StudentID";
        ocmd.Parameters.AddWithValue("@StudentID", StudentID);
        ocmd.Connection = oconn;
        ocmd.ExecuteNonQuery();
        oconn.Close();
        FillGrid();
    }
    protected void gv_student_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv_student.EditIndex = e.NewEditIndex;
            FillGrid();
    }
    protected void gv_student_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int StudentID = int.Parse(((Label)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("lbl_ID"))).Text);
        string StudentNo = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtno"))).Text;
        string StudentPassword = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtpassword"))).Text;
        string StudentName = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtname"))).Text;
        string StudentRollno = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtrollno"))).Text;
        string StudentSession = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtsession"))).Text;
        string StudentPhone = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtphone"))).Text;
        string StudentAddress = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtaddress"))).Text;
        string StudentEmail = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtemail"))).Text;
        string StudentReligion = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtreligion"))).Text;
        string StudentDOB = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtdob"))).Text;
        string StudentFees = ((TextBox)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("txtfees"))).Text;

        int StudentClassID = int.Parse(((DropDownList)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("ddlclassid"))).SelectedValue);
        int StudentSectionID = int.Parse(((DropDownList)(gv_student.Rows[e.RowIndex].FindControl("ddlsectionid"))).SelectedValue);
        int StudentGenderID = int.Parse(((DropDownList)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("ddlgenderid"))).SelectedValue);
        int StudentGuardianID = int.Parse(((DropDownList)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("ddlguardianid"))).SelectedValue);
        int StudentTransportID = int.Parse(((DropDownList)(gv_student.Rows[e.RowIndex].Cells[1].FindControl("ddltransportid"))).SelectedValue);
        SqlConnection oconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Final Year Project"].ToString());
        oconn.Open();
        SqlCommand ocmd = new SqlCommand();
        ocmd.CommandText = "UPDATE Student SET StudentNo=@StudentNo, StudentPassword=@StudentPassword, StudentName=@StudentName,StudentRollno=@StudentRollno,StudentSession=@StudentSession,StudentPhone=@StudentPhone,StudentAddress=@StudentAddress,StudentEmail=@StudentEmail,StudentReligion=@StudentReligion,StudentDOB=@StudentDOB,StudentFees=@StudentFees,StudentTransportID=@StudentTransportID,StudentGuardianID=@StudentGuardianID,StudentGenderID=@StudentGenderID,StudentSectionID=@StudentSectionID,StudentClassID=@StudentClassID WHERE StudentID=@StudentID";
        ocmd.Parameters.AddWithValue("@StudentID", StudentID);

        ocmd.Parameters.AddWithValue("@StudentClassID", StudentClassID);
        ocmd.Parameters.AddWithValue("@StudentSectionID", StudentSectionID);
        ocmd.Parameters.AddWithValue("@StudentGenderID", StudentGenderID);
        ocmd.Parameters.AddWithValue("@StudentGuardianID", StudentGuardianID);
        ocmd.Parameters.AddWithValue("@StudentTransportID", StudentTransportID);

        ocmd.Parameters.AddWithValue("@StudentFees", StudentFees);
        ocmd.Parameters.AddWithValue("@StudentDOB", StudentDOB);
        ocmd.Parameters.AddWithValue("@StudentReligion", StudentReligion);
        ocmd.Parameters.AddWithValue("@StudentEmail", StudentEmail);
        ocmd.Parameters.AddWithValue("@StudentAddress", StudentAddress);
        ocmd.Parameters.AddWithValue("@StudentPhone", StudentPhone);
        ocmd.Parameters.AddWithValue("@StudentSession", StudentSession);
        ocmd.Parameters.AddWithValue("@StudentRollno", StudentRollno);
        ocmd.Parameters.AddWithValue("@StudentName", StudentName);
        ocmd.Parameters.AddWithValue("@StudentPassword", StudentPassword);
        ocmd.Parameters.AddWithValue("@StudentNo", StudentNo);
        ocmd.Connection = oconn;
        ocmd.ExecuteNonQuery();
        gv_student.EditIndex = -1;
        FillGrid();
        oconn.Close();
    }
    protected void gv_student_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv_student.EditIndex = -1;
        FillGrid();
    }

0 个答案:

没有答案