-4

我在gridview中有这张表。

ID     Question_No          Question             Survey_ID
-----------------------------------------------------------

 1         1            Whats you name?             44
 2         2            How Old Are you?            44
 3         3       Whats your favorite hobby        44
 4         4          What did you study?           44 

我想在页面上添加一个删除按钮,如下所示:当我删除其中一个记录时,我想自动更新所有问题的 question_no,只要survey_ID 为 44。例如,如果我删除第二个问题,它会变成这样。

    ID     Question_No          Question             Survey_ID
-----------------------------------------------------------

     1         1            Whats you name?             44
     3         2       Whats your favorite hobby        44
     4         3          What did you study?           44 

我该怎么做呢?我认为它一定是一个循环,但我什至不知道如何处理它。

编辑:这是我的删除按钮代码

protected void RemoveQuestionButton_Click(object sender, EventArgs e)
        {
            try
            {
                DataRowView r;
                r = ((DataRowView)QuestionsGridView.GetRow(QuestionsGridView.FocusedRowIndex));
                Session["Question_ID"] = r[0];

                if (Session["Question_ID"] != null)
                {
                    SqlConnection connection = DatabaseConnection.GetSurveySystemConnection();
                    string delStatement1 = "DELETE FROM Questions WHERE ID =" + Session["Question_ID"];
                    string delStatement2 = "DELETE FROM Question_Options where Question_ID=" + Session["Question_ID"];
                    SqlCommand cmd = new SqlCommand(delStatement1, connection);
                    SqlCommand cmd2 = new SqlCommand(delStatement2, connection);
                    cmd.CommandType = CommandType.Text;
                    cmd2.CommandType = CommandType.Text;

                    try
                    {
                        cmd2.ExecuteNonQuery();
                        cmd.ExecuteNonQuery();
                        ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("Green");
                        ConfirmLbl.Text = "Question & Options Deleted Successfully!";
                        QuestionsGridView.DataBind();

                    }
                    catch (Exception)
                    {
                        ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        ConfirmLbl.Text = "This Question Has Options Linked to it...";
                    }
                    finally
                    {
                        connection.Close();

                    }
                }
            }
            catch (Exception)
            {
                ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                ConfirmLbl.Text = "You need to select a Question to edit...";
            }
        }
4

2 回答 2

2

这可以通过单个更新语句来完成。

delete from question
   where id = 2;

with new_order as (
   select row_number() over (partition by survey_id order by question_no) as new_question_no,
          question_no as old_question_no, 
          id
   from question
) 
update question 
  set question_no = nq.new_question_no
from new_order nq
where nq.id = question.id
  and survey_id = 44;

commit;

这是一个 SQLFiddle 示例:http ://sqlfiddle.com/#!6/0a1e7/1

于 2012-12-17T18:11:14.533 回答
1

您所需要的只是一个查询 db 表并将结果集设置/绑定到您的 gridview 的函数。然后,当您删除一个问题并将其从数据库中删除时,调用该函数并将您的 gridview 重新绑定到新的结果集。

您可以在 sql 查询中使用分区 ROW_NUMBER来创建行号的连续列。您可以查看其中的一些示例来帮助您执行此操作。

于 2012-12-17T18:41:21.567 回答