CAFE

ASP.NET 질문

Gridview에서 마지막 Row 삭제 후 처리방법

작성자HC.2PC|작성시간14.07.01|조회수513 목록 댓글 2

QnA

 

사용언어 (칠해주세요) => (C#,VB)

사용툴 (VS) : VS 2005

운영체제 (OS) : XP

상세언어(칠해주세요.) => 1) ASP.NET, 2) ASP.NET MVC, 3) ASP.NET AJAX



간만에 왔습니다.

역시나 질문만 올리고 빠르게 퇴장해야 할거 같네요 ㅎㅎㅎ;;


Gridview로 간단하게 원하는 데이터를 불러오고, 삭제하고, 수정하게끔 만들었습니다.


그런데 마지막 Row를 삭제한 다음에 다시 Listing 함수를 불러오면..

마지막 데이터를 계속 보여주네요..



소스는 아래와 같습니다.


string strSUSER_ID = 쿠키값;
            string strSDEPT_COD = 쿠키값;

            SqlConnection Con = new SqlConnection(ConnectionString);
            SqlCommand Cmd = new SqlCommand();

            Cmd.Connection = Con;

            Cmd.CommandText = "프로시져명";
            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.Parameters.AddWithValue("@strSDEPT_COD", strSDEPT_COD);
            Cmd.Parameters.AddWithValue("@txtStart", txtStart.Text + " 00:00");
            Cmd.Parameters.AddWithValue("@txtEnd", txtEnd.Text + " 23:59");
           

            try
            {
                SqlDataAdapter adp = new SqlDataAdapter(Cmd);
                DataTable dt = new DataTable();

                adp.Fill(dt);


                if (dt.Rows.Count > 0)
                {
                    DataView dv = new DataView();
                    dv = dt.DefaultView;

                    if (sortExp != string.Empty)
                    {
                        dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
                    }

                    GridView1.DataSource = dv;
                    GridView1.DataBind();
                }
            }

            catch (Exception ex)
            {
                lblError.Text = "ERROR : " + ex.Source + " - " + ex.Message;
                lblError.Visible = true;

            }


일단은 Listing 할때 불러오는 함수이고요,


// 삭제 버튼을 눌렀을 경우 동작함.
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "btnDelete")
            {
                string strBoard_id = e.CommandArgument.ToString();

                SqlConnection Con = new SqlConnection(ConnectionString);
                string strSql = 쿼리명;

                SqlCommand Cmd = new SqlCommand(strSql, Con);
                Cmd.Parameters.AddWithValue("@strBoard_id", strBoard_id);

                try
                {
                    Con.Open();
                    Cmd.ExecuteNonQuery();
                    Listing("", "");
                }

                catch (Exception ex)
                {
                    lblError.Text = "예외가 발생하였습니다. 사유는 다음과 같습니다. <br />";
                    lblError.Text += ex.Message;
                }

                finally
                {
                    Con.Close();
                }
            }
        }


이런식으로 RowCommand를 사용해서해당 Row를 삭제하고, Listing을 다시합니다.

꼭 마지막 Row만 삭제하면 다시 불러지지가 않네요 ㅎㅎ


어떻게하면 좋을까요?


그냥 Respons.Redirect하면 될꺼 같기는 한데... 이게 마지막 행이란걸 체크는 어떻게 할까요?ㅎㅎ

다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
  • 작성자퓨전마법사 | 작성시간 14.07.01 일단.. 마지막행일때만 Redirect 하는건 꼼수 같구요.. 정공법으로 왜 마지막행만 삭제하면 안돼는지를 찾아서 해결하는것이
    급선무일것 같습니다.
    소스상으로는 문제 없어 보이고. 캐쉬 때문인거 같기도 하구요. 저같은경우는 익스의 도구 - 옵션 - 임시인터넷 파일 - 페이지 열때마다로 해놓습니다. 그리고 GridView1_RowCommand 함수에서 Listing("", ""); <-- 리스트 바인딩 시키는 함수 호출하기 전에 GridView1 초기화 해보세요.. 소스상에는 문제 없어 보여서 이렇게 밖에는 말씀 드릴것이 없네요.
  • 답댓글 작성자HC.2PC 작성자 본인 여부 작성자 | 작성시간 14.07.02 감사합니다.

    아래와 같이 Row <= 1 행일경우에만 DataBind를 다시해서 해결했네요 ㅎ

    // 마지막 Row일 경우에만 DataBind를 다시하여 초기화한다.
    if (this.GridView1.Rows.Count <= 1)
    {
    GridView1.DataBind();
    }

    // 그렇지 않을경우는 바로 Listing
    else
    {
    Listing("", "");
    }
댓글 전체보기
맨위로

카페 검색

카페 검색어 입력폼