我正在网站上购买购物车,并且我在arraylist中有我的商品(已添加到购物车中)。使用这些值我已经从db中读取了其余的值,并且已成功填充在gridview上。
if (d1.Read())
{
d1.Close();
sda.SelectCommand = searchResult;
sda.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
如何在此gridview中添加名为“Quantity”的其他列以及文本框,以便用户可以为gridview中显示的每个项目输入qunatity的值?
非常感谢任何帮助/建议/链接。
感谢,
//编辑2
<asp:GridView ID="GridView2" runat="server" BackColor="#388AD0"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" Height="160px" Style="left: 72px;
position: relative; top: 8px" Width="504px" AllowPaging="True" PageSize="5">
<FooterStyle BackColor="#E0E0E0" />
<PagerStyle BackColor="Silver" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#388AD0" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#388AD0" />
<PagerSettings Mode="NextPrevious" />
</asp:GridView>
//编辑3
public void additem(string additem)
{
DataTable dt = new DataTable();
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ewap_k.mdf;Integrated Security=True;User Instance=True";
using (SqlConnection searchCon = new SqlConnection(ConnectionString))
{
using (SqlCommand searchResult = new SqlCommand("SELECT ItemID, Name, RelDate, Price, Status FROM item_k WHERE ItemID IN ( " + itemIDs + ")", searchCon))
{
searchCon.Open();
SqlDataReader d1 = searchResult.ExecuteReader();
SqlDataAdapter sda = new SqlDataAdapter();
if (d1.Read())
{
d1.Close();
sda.SelectCommand = searchResult;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
else { }
searchCon.Close();
}
}
}
//编辑4
<asp:GridView ID="GridView1" runat="server" Style="left: 8px; position: relative;
top: 0px">
<Columns>
<asp:BoundField DataField="ItemID" Visible="false"/>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="RelDate" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" />
<asp:BoundField DataField="Status" />
</Columns>
</asp:GridView>
答案 0 :(得分:3)
您可以添加模板字段并将文本框放在ItemTemplate中:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
[编辑]这是一个更完整的网格:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField />
<asp:BoundField />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
请注意,只要我决定将其放在列列表中,TemplateField就会显示出来。如果这仍然不适合你,你可以发布你的GridView标记,这样我就可以看到你有什么?
[编辑2]这是您网格的更新版本。您只需要设置BoundFields以绑定到您需要的数据集中的任何值。
<asp:GridView ID="GridView2" runat="server" BackColor="#388AD0" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
Height="160px" Style="left: 72px; position: relative; top: 8px" Width="504px"
AllowPaging="True" PageSize="5">
<Columns>
<asp:BoundField />
<asp:BoundField />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#E0E0E0" />
<PagerStyle BackColor="Silver" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#388AD0" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#388AD0" />
<PagerSettings Mode="NextPrevious" />
</asp:GridView>