我想将列表中的值绑定到gridview行。
我尝试使用下面的代码,但是它绑定了所有星期一字段中列表的最后一个值。
代码.aspx:
<asp:GridView ID="gridActivity" runat="server" AllowSorting="true" AutoGenerateColumns="false"
CellPadding="5" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84" Height="171px" style="margin-left: 0px; margin-right: 0px" Width="780px">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle Width="10px" Wrap="false" ForeColor="White" Font-Bold="True"
BackColor="SteelBlue"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Activities">
<ItemTemplate>
<asp:Label ID="lblActivity" runat="server" Text='<%#Eval("ActivityName") %>' CssClass="columnStyle"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monday">
<ItemTemplate>
<asp:TextBox ID="txtMon" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday">
<ItemTemplate>
<asp:TextBox ID="txtTue" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday">
<ItemTemplate>
<asp:TextBox ID="txtWed" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Thursday">
<ItemTemplate>
<asp:TextBox ID="txtThu" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Friday">
<ItemTemplate>
<asp:TextBox ID="txtFri" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday">
<ItemTemplate>
<asp:TextBox ID="txtSat" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday">
<ItemTemplate>
<asp:TextBox ID="txtSun" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Width ="100%"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
代码.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DbConnection.Open();
gridActivity.DataSource = Application["listActivity"];
gridActivity.DataBind();
DbConnection.Close();
}
if (Application["mondayValues"] != null)
{
//Here I can get all the values
foreach (string mon in Application["mondayValues"] as List<string>)
{
//Here is the problem
foreach (GridViewRow row in gridActivity.Rows)
{
TextBox txtMon = (TextBox)row.FindControl("txtMon");
txtMon.Text = mon;
}
}
}
}
在下面的代码中,我可以获取所有星期一的行值并存储到Application [“monday”]
中 protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in gridActivity.Rows)
{
TextBox txtMon = (TextBox)row.FindControl("txtMon");
Application["mondayValues"] = txtMon.Text;
mondayData = new MondayData { MondayValue = txtMon.Text };
listMonday.Add(txtMon.Text);
}
Application["mondayValues"] = listMonday;
}
答案 0 :(得分:0)
您的问题是您为所有网格线重复每条数据线的写入过程。这样,您的最后一个值将被写入所有网格线。
//Here I can get all the values
foreach (string mon in Application["mondayValues"] as List<string>)
{
foreach (GridViewRow row in gridActivity.Rows)
{
// PSEUDO CODE
// if ( row.Text != null )
// next row
// else
// Fill Row with text and break foreach
}
}