2012-11-28 35 views
0

我在GridView中有TextBoxLabel。我的問題是,當我更新GridView並且TextBox爲空時,它會刪除標籤中的數據(這很有意義)。我的問題是,是否有可能保持TextBox爲空並更新而不丟失任何數據?TextBox刪除GridView數據爲空

C#:

Private void Update() 
    { 
     StringBuilder sb = new StringBuilder(); 

     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      TextBox timeR = GridView1.Rows[i].FindControl("SitUps") as TextBox; 
      if (timeR.Text.Length < 10) 
      { 
       foreach (GridViewRow row in GridView1.Rows) 
       { 
        sb.Append("UPDATE bleaTest SET SitUps = '"); 
        sb.Append((row.FindControl("SitUps") as TextBox).Text); 
        sb.Append("'"); 
        sb.Append(" WHERE id = "); 
        sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text)); 
        sb.Append(" "); 

       } 
      } 
      else 
      { 
       timeR.Text = "Number is too high!"; 
       foreach (GridViewRow row in GridView1.Rows) 
       { 
        sb.Append("UPDATE bleaTest SET SitUps = '"); 
        sb.Append((row.FindControl("SitUps") as TextBox).Text); 
        sb.Append("'"); 
        sb.Append(" WHERE id = "); 
        sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text)); 
        sb.Append(" "); 

       } 

      } 
     } 



     string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=v2soft;Password=passwordv2soft"; 
     SqlConnection myConnection = new SqlConnection(connectiongString); 
     SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection); 
     myConnection.Open(); 
     myCommand.ExecuteNonQuery(); 
     myConnection.Close(); 
     BindData(); 
     } 

ASPX:

<asp:TemplateField HeaderText="Sit Ups"> 
     <ItemTemplate> 
      <asp:TextBox ID="SitUps" runat="server" type="number" Text='<%# Eval("SitUps") %>' EnableViewState="True"></asp:TextBox></div> 

     </ItemTemplate> 
    </asp:TemplateField> 
+0

如果在更新過程中文本框爲空,您究竟想要更新什麼?請稍微描述一下。 – ORION

+0

嗨jcalabris,如果'TextBox'是空的我希望它什麼也不做,而是將一個空白數據放入表中。讓我知道這是否合理。 – vadim

+1

gridview是否應該只有一行,或者它可以有多行?這背後的基本思想是隻更新自上次更新以來已更改的行。是否有可能該行可以更改並設置爲空值?在那種情況下,我猜你會需要更新數據庫嗎? – ORION

回答

0

我想通了,這裏的問題是代碼:

StringBuilder sb = new StringBuilder(); 


       foreach (GridViewRow row in GridView1.Rows) 
       { 
        TextBox tR = row.FindControl("SitUps") as TextBox; 
        if(tR.Text != "") 
        { 
        sb.Append("UPDATE bleaTest SET SitUps = '"); 
        sb.Append((row.FindControl("SitUps") as TextBox).Text); 
        sb.Append("'"); 
        sb.Append(" WHERE id = "); 
        sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text)); 
        sb.Append(" "); 
        string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=v2soft;Password=passwordv2soft"; 
        SqlConnection myConnection = new SqlConnection(connectiongString); 
        SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection); 
        myConnection.Open(); 
        myCommand.ExecuteNonQuery(); 
        myConnection.Close(); 

        } 

       } 
     BindData(); 
     } 

我清理了代碼,現在它檢查文本是否爲空並繼續更新。

謝謝!

1

你可以改變if (timeR.Text.Length < 10)if (timeR.Text.Length < 10 and timeR.Text.Length > 0)

+1

在另一個說明中,您會意識到您正循環遍歷整行行中的整個循環內部循環。你有for fore的foreach,遍歷所有行。 – gashach

+0

對不起,我是這麼亂的代碼,我沒有注意到這一點。 – vadim