2013-09-25 49 views
1

我有一个具有sqldatasource的gridview。我的数据应该是这样的:Gridview检查更新前的所有行

Desired

但在任何行,当我点击编辑按钮,然后选择“更新”按钮,我要检查它的上一行“Ending_Price”必须小于该行的Start_Price。此行的End_Price必须小于下一行的起始价格。

如果这没有发生,那么必须通知用户,因为此范围必须是有序的。

这里是我的代码:

 protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) 
    { 


     if (e.RowIndex == 0 && GridView1.Rows.Count != 1) 
     { 
      // Next Row Starting Value 
      TextBox txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange") as TextBox; 

      // This row ending value 
      TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox; 

      if (Convert.ToDecimal(txtThisEndingRange.Text) >= Convert.ToDecimal(txtNextStartingPoint)) 
      { 
       NotificationHelper.ShowError(this, "Invalid Ending Value"); 
       e.Cancel = true; 
       return; 
      } 

     } 

     TextBox txtStartingPoint = GridView1.Rows[e.RowIndex].FindControl("txtStartingRange") as TextBox; 
     TextBox txtEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox; 
     TextBox txtDiscount_Percentage = GridView1.Rows[e.RowIndex].FindControl("txtDiscount_Percentage") as TextBox; 
     Label txtDiscount_ID = GridView1.Rows[e.RowIndex].FindControl("lblId") as Label; 


     SqlDataSource1.UpdateParameters["sr"].DefaultValue = txtStartingPoint.Text; 
     SqlDataSource1.UpdateParameters["re"].DefaultValue = txtEndingRange.Text; 
     SqlDataSource1.UpdateParameters["perc"].DefaultValue = txtDiscount_Percentage.Text; 
     SqlDataSource1.UpdateParameters["discount_ID"].DefaultValue = txtDiscount_ID.Text; 


     SqlDataSource1.Update(); 
    } 



    /// <summary> 
    /// Edit record 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void editRecord(object sender, GridViewEditEventArgs e) 
    { 
     // Get the current row index for edit record 
     GridView1.EditIndex = e.NewEditIndex; 

    } 


    protected void cancelRecord(object sender, GridViewCancelEditEventArgs e) 
    { 
     GridView1.EditIndex = -1; 
    } 

这会返回一个空值:

TextBox txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange") as TextBox; 

这里是我的GridView标记

<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" 
       OnRowCancelingEdit="cancelRecord" OnRowEditing="editRecord" OnRowUpdating="UpdateRecord" 
       CellPadding="4" GridLines="None" Width="673px" ForeColor="#333333" 
       DataSourceID="SqlDataSource1" OnDataBound="GridView1_DataBound" 
       > 
       <RowStyle HorizontalAlign="Center" /> 
       <AlternatingRowStyle BackColor="White" /> 
       <EditRowStyle BackColor="#7C6F57" /> 
       <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" /> 
       <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#E3EAEB" /> 
       <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> 
       <Columns> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          Id</HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblId" runat="server" Text='<%#Bind("discount_id")%>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          Starting Point</HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblStartingRange" runat="server" Text='<%#Bind("starting_range") %>'></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtStartingRange" runat="server" Text='<%#Bind("starting_range") %>' 
           MaxLength="50"></asp:TextBox> 
         </EditItemTemplate> 
         <FooterTemplate> 
          <asp:TextBox ID="txtNewStartingRange" runat="server" MaxLength="50"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          Ending Range</HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblEndingRange" runat="server" Text='<%#Bind("ending_range") %>'></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtEndingRange" runat="server" Text='<%#Bind("ending_range") %>' 
           MaxLength="2"></asp:TextBox> 
         </EditItemTemplate> 
         <FooterTemplate> 
          <asp:TextBox ID="txtEndingRange" runat="server" MaxLength="2"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          Discount Percentage</HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lbldiscount_percentage" runat="server" Text='<%#Bind("discount_percentage") %>'></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtDiscount_Percentage" runat="server" Text='<%#Bind("discount_percentage") %>' 
           MaxLength="10"></asp:TextBox> 
         </EditItemTemplate> 
         <FooterTemplate> 
          <asp:TextBox ID="txtNewDiscount_Percentage" runat="server" MaxLength="10"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          Operation</HeaderTemplate> 
         <ItemTemplate> 
          <asp:Button ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" /> 
          <asp:Button ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" CausesValidation="true" 
           OnClientClick="return confirm('Are you sure?')" /> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" /> 
          <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" /> 
         </EditItemTemplate> 
         <FooterTemplate> 
         </FooterTemplate> 
        </asp:TemplateField> 
       </Columns> 
       <EmptyDataTemplate> 
        No record available 
       </EmptyDataTemplate> 
      </asp:GridView> 

我应该怎么做呢?任何想法?

+0

展GridView标记 –

+0

@FlopScientist请检查编辑 –

回答

1

其他查询返回像这样的值?

// This row ending value 
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox; 

or are they also null?

你可以试着不要试试这个值。

var txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange") 

也许它不是一个文本框你回来。

+1

txtThisEndingRange返回正在编辑的当前行的正确值...但是txtNextStartingPoint为null –

1

有2分被照顾的:

1),这是正在编辑当前行(行的获取值) 在这种情况下,该值是内部TextBox控制中,由于行处于现在编辑模式。

// This row ending value 
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox; 

2.)乘车旁边当前行正在编辑) 在这种情况下,从下一行(行值,行值是内部Label控制,因为行是在没有模式Edit但在Normal模式。

这意味着<ItemTemplate>控件当前由GridView呈现,而不是由<EditItemTemplate>控件呈现。

所以现在你可以猜测,你不需要找TextBox: txtStartingRange,但你必须得到Label: lblStartingRange

// Next Row starting Value 
Label lblNextStartRange= GridView1.Rows[e.RowIndex + 1].FindControl("lblStartingRange") as Label; 

现在做你的正常检查/值作为比较:

if (Convert.ToDecimal(txtThisEndingRange.Text) >= Convert.ToDecimal(lblNextStartRange.Text)) 
{ 
.. 
.. 
}