C# Gridview在更新前检查所有行
我有一个gridview,它有一个sqldatasource。我的数据应该是这样的: 但在任何一行上,当我单击“编辑”按钮,然后单击“更新”按钮时,我想检查它的前一行“结束价格”必须小于此行的开始价格。此外,此行的结束价格必须小于下一行的开始价格 如果未发生这种情况,则必须通知用户,因为此范围必须符合顺序 这是我的密码:C# Gridview在更新前检查所有行,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,它有一个sqldatasource。我的数据应该是这样的: 但在任何一行上,当我单击“编辑”按钮,然后单击“更新”按钮时,我想检查它的前一行“结束价格”必须小于此行的开始价格。此外,此行的结束价格必须小于下一行的开始价格 如果未发生这种情况,则必须通知用户,因为此范围必须符合顺序 这是我的密码: protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) {
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;
}
这是我的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>
身份证件
起点
终点范围
折扣率
活动
没有可用的记录
我该怎么办?有什么想法吗?其他查询返回这样的值
// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;
或者它们也是空的
您可以尝试不强制转换该值。请尝试此操作
var txtNextStartingPoint = GridView1.Rows[e.RowIndex + 1].FindControl("txtStartingRange")
也许你得到的不是一个文本框。有两点需要注意: 1.)获取当前行(正在编辑的行)的值 在这种情况下,值位于
文本框
控件内,因为行现在处于编辑模式。
// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;
2.)从下一行(正在编辑的当前行旁边的行)获取值
在这种情况下,行值位于标签
控件内,因为行不处于编辑
模式,而是处于正常
模式。
// This row ending value
TextBox txtThisEndingRange = GridView1.Rows[e.RowIndex].FindControl("txtEndingRange") as TextBox;
这意味着
控件当前由GridView呈现,而不是
控件
因此,现在您可以猜测,您不需要查找TextBox:txtStartingRange
,但必须获得标签: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))
{
..
..
}
TXThisEndingRange正在返回正在编辑的当前行的正确值。。。但是txtNextStartingPoint是null@FlopScientist请检查编辑