GridView中实现列的绑定
-------------前台GridView--------------------
<asp:GridView ID="CartGrid" runat="server" AutoGenerateColumns="False" Width="100%" EmptyDataText="<strong><center><font color='Red'>您的购物车为空!</font></center></strong>" SkinID="ShopCarGridView" OnRowDataBound="CartGrid_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<input value='<%# Eval("Id") %>' name="Choice" type="checkbox" checked="true" />
</ItemTemplate>
<ItemStyle Width="28px" />
<HeaderStyle Width="28px" />
</asp:TemplateField>
<asp:BoundField DataField="ProductId" HeaderText="商品编号" >
<ItemStyle HorizontalAlign="Center" Width="80px" />
<HeaderStyle HorizontalAlign="Center" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="商品名称" />
<asp:TemplateField HeaderText="所得积分">
<ItemTemplate>
<%#GetTotalScore(Eval("Score").ToString(), Eval("Quantity").ToString())%>
</ItemTemplate>
<ItemStyle Width="55px" />
<HeaderStyle Width="55px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="运输费用">
<ItemTemplate>
<%# Eval("SendPrice","{0:c}") %>
</ItemTemplate>
<ItemStyle Width="55px" />
<HeaderStyle Width="55px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="配送方式">
<ItemTemplate>
<asp:Label ID="lblSend" runat="server" Text=""></asp:Label>
<%--<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>--%>
</ItemTemplate>
<ItemStyle Width="60px" />
<HeaderStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="订购价格">
<ItemTemplate>
<%# Eval("Price","{0:c}") %>
<asp:HiddenField ID="hdId" Value='<%# Eval("Id") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="55px" />
<HeaderStyle Width="55px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("Quantity") %>' Width="30px" MaxLength="6" style="line-height:110%;background:#374E98;color:White" onblur="test(this.value);"></asp:TextBox><asp:RegularExpressionValidator ID="RQuantity" runat="server" ErrorMessage="数量只能是正整数" ControlToValidate="txtQuantity" SetFocusOnError="True" ValidationExpression="^[1-9]\d*$">*</asp:RegularExpressionValidator>
</ItemTemplate>
<ItemStyle Width="40px" VerticalAlign="Middle" />
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="小计">
<ItemTemplate>
<%#GetTotalPriceEx(Eval("Price").ToString(), Eval("Quantity").ToString(),Eval("SendPrice").ToString())%>
</ItemTemplate>
<ItemStyle Width="40px" />
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<a href="ShopCar.aspx?action=Delete&id=<%# Eval("Id") %>" onclick="return confirm('确定从购物车中删除该商品吗?') " ><span style='color:blue' >删除</span></a>|<a href="Fav.aspx?Action=add&ProductID=<%# Eval("Id") %>" onclick="return confirm('是否把该商品加入到收藏夹?') " ><span style='color:blue' >收藏</span></a>
</ItemTemplate>
<ItemStyle Width="60px" />
<HeaderStyle Width="60px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
----------------后台cs文件的绑定-------------------------
protected void BindShoppingCart()
{
DataBase db = new DataBase();
conn = db.GetConn();
string SQL="select * from V_Order";
SqlDataReader sdr = db.RunSql(SQL,conn);
CartGrid.DataSource = sdr;
CartGrid.DataBind();
sdr.Close();
db.dispose(conn);
}
protected void CartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.FindControl("hdId") != null)
{
HiddenField hh1 = (HiddenField)e.Row.FindControl("hdId");
string GetSendMethodBYId = hh1.Value.ToString();
conn = db.GetConn();
string SQL = "SELECT F_SendWayName,F_SendMethodID FROM V_ProductSendWay WHERE (F_ProductId = '" + GetSendMethodBYId + "')";
SqlDataReader myReader = db.RunSql(SQL, conn);
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
Label lbl = (Label)e.Row.FindControl("lblSend");
lbl.Text = "<select id='selSend' name='selSend'>";
while (myReader.Read()) {
lbl.Text += "<option value='" + myReader[1].ToString() + "'>" + myReader[0].ToString() + "</option>";
}
lbl.Text += "</select>";
myReader.Close();
}
}