2012-12-17 60 views
-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 

我想一个删除按钮添加到工作原理是这样的页面:当我删除这些记录中的一个,我想只要survey_ID是44.自动更新的所有问题的question_no例如,如果我删除第二个问题,它会变成这样。

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..."; 
      } 
     } 
+2

[你有什么尝试?](http://whathaveyoutried.com) – 2012-12-17 18:06:42

+2

如果他不知道如何接近它,似乎没有任何东西。 –

+1

你有任何你自己试过的C#代码..?提供你一个快速的答案并不真的有助于你学习C#请花时间谷歌和或尝试编码自己第一.. – MethodMan

回答

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

+0

Iam使用sql server 2012 – BasharKH

+0

@BasharKH:看我的编辑。 –

+0

谢谢你,这工作:D – BasharKH

1

所有你需要的是查询数据库表并设置功能/结合的结果设置为你的GridView。然后,当你删除一个问题并从数据库中删除它时,调用该函数并将你的gridview重新绑定到新的结果集。

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

+0

那么,我想添加删除语句下的选择和更新语句,当我删除它会检查问题的订单号码,然后再次设置它们,但问题是如何我可以更新一列的值:1,2,3,4等,直到列结束? – BasharKH

+0

查看我的更新:) –