none
两个GridView之间的数据转移 RRS feed

  • 问题

  • 在今天做系统的过程中遇到了如下问题:是学生选课页面,共有两个GridView,一个是GridView1,用来显示可选修的课程;另一个是GridView2,用来显示您已经选修的课程。我的想法是这样的在GridView1中点击选修按钮后,该行的记录被移到GridView2中,同时该行记录在GridView1中删除;同时当我点击GridView2中的退选按钮时,该行记录在GridView2中消失,同时显示在GridView1中,我已经用不同的SQL语言对这两个GridView进行了绑定,其中GridView1中的DataSourceID为SqlDataSource1;GridView2中的DataSourceID为SqlDataSource2;
    我的后台代码初步为:
    public partial class StudentCourseElect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "select")                                      //如果单击“选修”超链接
            {
                int index = Convert.ToInt32(e.CommandArgument);                 //取出选修课程所在的行索引
                DataKey key = GridView1.DataKeys[index];                        //创建DataKey集合接收该行的主键
                string CourseTeacherID = GridView1.DataKeys[index].Values[0].ToString().Trim();//取出课程教师编号的主键值
                string CourseID = GridView1.DataKeys[index].Values[1].ToString().Trim();       //取出课程编号主键值
                string TeacherID = GridView1.DataKeys[index].Values[2].ToString().Trim();      //取出教师编号的主键值
                string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(connStr);
                //查询该课程是否已经选择
                string SqlExist = "select ECT.StudentID,ECT.CourseTeacherID,CT.CourseID from ElectCourseTable as ECT,CourseTable as CT where ECT.CourseTeacherID=CT.CourseTeacherID and CT.CourseID='" + CourseID + "'and StudentID='" + Session["StudentID"] + "'";
                string Sqlinsert = "insert into ElectCourseTable(StudentID,CourseTeacherID) values('" + Session["StudentID"] + "','" + CourseTeacherID + "')";
                string Sqldelete = "delete from ElectCourseTable where StudentID='" +Session["StudentID"]+ "'and CourseTeacherID='" + CourseTeacherID + "'";
                SqlCommand cmdSqlExist = new SqlCommand(SqlExist, conn);
                try
                {
                    if (conn.State.ToString() == "Closed")
                        conn.Open();                                           //打开连接
                    SqlDataReader sdrSqlExist = cmdSqlExist.ExecuteReader();   //执行查询
                    if (sdrSqlExist.Read())                                  //如果该用户存在
                    {
                        Response.Write("<script language=javascript>alter('您已经选择了该门课程,该门课程由不同老师授课,如果想选择该老师的此门课程,请退去其他老师教授的同名课程')</script>");
                        sdrSqlExist.Close();
                    }
                    else
                    {
                        sdrSqlExist.Close();
                    }
                    if (conn.State.ToString() == "Closed")
                        conn.Open();
                    SqlCommand cmdinsert = new SqlCommand(Sqlinsert, conn);
                    cmdinsert.ExecuteNonQuery();                           //执行更新
                    cmdinsert.Dispose();
                    SqlCommand cmdstr = new SqlCommand(Sqldelete, conn);      //执行更新
                    cmdstr.ExecuteNonQuery();
                    cmdstr.Dispose();
                    if (conn.State.ToString() == "Open")
                        conn.Close();

                }
                catch (Exception ex)                                     //异常处理
                {
                    Response.Write("数据库错误,错误原因:" + ex.Message);
                    Response.End();
                }
                finally
                {
                    if (conn.State.ToString() == "Open")
                        conn.Close();
                }
                GridView1.DataBind();
                GridView2.DataBind();
               

            }

       
        }
        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "tuixuan")                                                              //如果单击“退选”超链接
            {
                int index = Convert.ToInt32(e.CommandArgument);                                      //取出选修课程所在的行索引
                DataKey key = GridView2.DataKeys[index];                                              //创建DataKey集合接收该行的主键
                string CourseTeacherID = GridView2.DataKeys[index].Values[0].ToString().Trim();    //取出课程教师编号的主键值
                string CourseID = GridView2.DataKeys[index].Values[1].ToString().Trim();            //取出课程编号的主键值
                string TeacherID = GridView2.DataKeys[index].Values[2].ToString().Trim();          //取出教师编号的主键值
                string StudentID = Session["StudentID"].ToString();
                string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                string SqlStr = "delete from ElectCourseTable where StudentID='" + StudentID + "'and CourseTeacherID='" + CourseTeacherID + "'";
                try
                {
                    SqlConnection conn = new SqlConnection(connStr);                                     //创建连接对象
                    if (conn.State.ToString() == "Closed")                                                //如果连接关闭,则打开连接
                        conn.Open();
                    SqlCommand comm = new SqlCommand(SqlStr, conn);
                    comm.ExecuteNonQuery();                                                           //插入选修课程
                    comm.Dispose();
                    if (conn.State.ToString() == "Open")                                                  //如果连接打开,则关闭连接
                        conn.Close();

                }
                catch (Exception ex)
                {
                    Response.Write("数据库错误,错误原因:" + ex.Message);
                    Response.End();
                }
                GridView1.DataBind();
                GridView2.DataBind();
            }
        }}
    我想问的是,如何修改我的后台代码才能实现我的上述要求,由于我对使用自定义的子查询语句来对数据进行绑定掌握得还不是很娴熟,所以……请各位前辈帮帮忙!(我运行了一下,GridView2不能显示出来。)
    其中两个GridView中的DataKeys均为(CourseTeacherID——授课班号,CourseID,TeacherID)
    由于此段代码是先前编写的,所以使用的拼串的SQL语句,以后我会注意的,请见谅!
    前台的数据源绑定代码为:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT CT.CourseID, TT.TeacherID, CT.CourseTeacherID, CT.CourseName, TT.TeacherName, CT.CourseTime, CT.CourseAddress FROM TeacherTable AS TT INNER JOIN CourseTable AS CT ON TT.TeacherID = CT.TeacherID WHERE (CT.CourseTeacherID NOT IN (SELECT CourseTeacherID FROM ElectCourseTable WHERE (StudentID = @StudentID)))">
                <SelectParameters>
                    <asp:SessionParameter DefaultValue="Session["StudentID"].ToString()" Name="StudentID"
                        SessionField="Session[&quot;StudentID&quot;]" />
                </SelectParameters>
            </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select CT.CourseID,CT.CourseName,CT.CourseTeacherID,CT.CourseTime,CT.CourseAddress,TT.TeacherID,
           TT.TeacherName,ECT.StudentID
    from CourseTable as CT,TeacherTable as TT,ElectCourseTable as ECT
    where CT.CourseTeacherID=ECT.CourseTeacherID
          and TT.TeacherID=CT.TeacherID
          and StudentID=@StudentID">
                <SelectParameters>
                    <asp:SessionParameter DefaultValue="1" Name="StudentID"
                        SessionField="Session[&quot;StudentID&quot;]" />
                </SelectParameters>
            </asp:SqlDataSource>

    2010年2月6日 10:19

答案

全部回复

  • GridView2没显示出来,可能是GridView2没有数据,
    也可能是你绑定的问题,你在page_load中加入
    if(!ispostback),当页面第一次家载的时候也进行绑定
    努力+方法=成功
    2010年2月6日 10:29
  • 版主,不好意思,我还没有明白,可不可以劳烦您具体讲解一下,由于这边不是太熟悉,所以希望您不吝赐教,麻烦了
    2010年2月6日 10:42
  • 你在里面加载是也绑定
     protected void Page_Load(object sender, EventArgs e)
        {

        if(!ispostback)
      {
            GridView1.DataBind();
                GridView2.DataBind();

      }
    }


    努力+方法=成功
    2010年2月6日 10:55
  • 前辈,您好!我按您的方法调试还是不行的,我重新用代码编写了GridViewBind()您帮我看看我写的这个代码正不正确,我顺带调试了一下,出现了这个错误:数据库错误,错误原因:“GridView2”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。
    我不知道该怎么修改:
      //GridView  绑定数据的方法
        private void GridViewBind()
        {
            string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            string SqlStr = @"select CT.CourseTeacherID,CT.CourseID,CT.TeacherID,CT.CourseName,CT.CourseTime,CT.CourseAddress,TT.TeacherName
                            from CourseTable as CT,TeacherTable as TT
                            where TT.TeacherID=CT.TeacherID
                            order by CourseTeacherID";
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                if (conn.State.ToString() == "Closed")
                    conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
                da.Fill(ds);
                GridView2.DataSource = ds.Tables[0].DefaultView;
                GridView2.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误,错误原因:" + ex.Message);
                Response.End();
            }
            finally
            {
                if (conn.State.ToString() == "Open")
                    conn.Close();
            }
        } 

    2010年2月6日 11:33
  • 出现错误的原因是GridView2有两个数据源,是你绑定的问题
    努力+方法=成功
    2010年2月6日 11:49
  • 问题已经解决!

    • 已标记为答案 Smiling008 2010年2月8日 6:14
    2010年2月8日 6:14