我的网络表单中有两个文本框和一个gridview。 gridview与数据库绑定。但是我想在运行时添加一个列,它将是webform的文本框输入。因为场景如下:我维护两个公式来使用两个文本框计算一些百分比,并且客户端想要在gridview中查看每一行的计算。
但我不能这样做。
有没有人可以帮我这个?可能是一些建议。
提前致谢。
答案 0 :(得分:2)
您可以使用标签控件在GridView标记中添加列,以显示结果,如下所示。
这是所需的标记,请注意Visible设置为false。
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
使用RowDataBound事件查找标签并计算结果如下:
void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find the control
var label1 = e.Item.FindControl("label1") as Label;
if (label1 != null)
{
if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
{
// Do the calculation and set the label
label1.Text = tbInput1.Text + tbInput2.Text;
// Make the column visible
GridView1.Columns[0].Visible = true;
}
}
}
}
请原谅任何错误,我没有测试过上述情况。