哭求:关于在GridView增加一行,然后在GridView模板列动态绑定下拉框的值问题
ASPX页面的代码:
<asp:GridView ID="gv_Order" runat="server" AutoGenerateColumns="False" Width="921px" DataKeyNames="OrderCode" OnRowCreated="gv_Order_RowCreated">
<Columns>
<asp:TemplateField HeaderText="订单编号">
<ItemTemplate>
<asp:TextBox ID="txtOrderCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OrderCode")%>' Width="131px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="产品名称">
<ItemTemplate>
<asp:DropDownList ID="ddlP" runat="server" AutoPostBack=true SelectedValue='<%# DataBinder.Eval(Container.DataItem, "PCode")%>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="订购数量">
<ItemTemplate>
<asp:TextBox ID="txtNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Number")%>' Width="69px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="优惠率">
<ItemTemplate>
<asp:TextBox ID="txtDiscount" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Discount")%>' Width="61px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="订货日期">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Date")%>' Width="131px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="送货日期">
<ItemTemplate>
<input id="txtTime" runat="server" name="Text1" onfocus="calendar()" onkeypress="return false"
onselectstart="return false;" readonly="readonly" style="width: 131px" type="text" value='<%# DataBinder.Eval(Container.DataItem, "DeliveGoodsDate")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="出库状态">
<ItemTemplate>
<asp:DropDownList ID="ddlS" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "M_IsStock")%>'>
<asp:ListItem Value="1">已出库</asp:ListItem>
<asp:ListItem Value="0">未出库</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="收款状态">
<ItemTemplate>
<asp:DropDownList ID="ddlM" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "M_IsMoney")%>'>
<asp:ListItem Value="1">已收款</asp:ListItem>
<asp:ListItem Value="0">未收款</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
CS页面代码:
BLL.TC_ClientInfo bll = new BLL.TC_ClientInfo();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTestData();
}
}
private void DDListM()
{
//绑定DropDownList
//drlP.Items.Clear();
DataTable dtM = bll.GetTP_ProductInfo();
ddlP.DataSource = dtM;
ddlP.DataTextField = "Name";
ddlP.DataValueField = "Code";
ddlP.DataBind();
}
protected void BindTestData()
{
string strCode = "";
//读取此用户以前订单信息数据绑定gv_Order
DataTable dt1 = new DataTable("TableOrder");
dt1 = bll.GetTS_OrderInfo(strCode);
this.gv_Order.DataSource = dt1;
this.gv_Order.DataBind();
}
protected DataTable GetDataFromGrid()
{
string strCode = "";
DataTable dt1 = new DataTable("TableOrder");
dt1 = bll.GetTS_OrderInfo(strCode);
for (int i = 0; i < gv_Order.Rows.Count; i++)
{
GridViewRow gRow = gv_Order.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] = ((TextBox)gRow.FindControl("OrderCode")).Text;
newRow[1] = ((DropDownList)gRow.FindControl("PCode")).
newRow[2] = ((TextBox)gRow.FindControl("Number")).Text;
newRow[3] = ((TextBox)gRow.FindControl("Discount")).Text;
newRow[4] = DateTime.Now.ToString();
newRow[5] = ((TextBox)gRow.FindControl("txtTime")).Text;
newRow[6] = ((DropDownList)gRow.FindControl("ddlS")).SelectedValue;
newRow[7] = ((DropDownList)gRow.FindControl("ddlM")).SelectedValue;
dt1.Rows.Add(newRow);
}
dt1.AcceptChanges();
return dt1;
}
protected void btnPr_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
//newRow["PCode"] = "1";
newRow["OrderCode"] = bll.GetNewIDOrder();
newRow["M_IsStock"] = "0";
newRow["M_IsMoney"] = "0";
dt.Rows.Add(newRow);
this.gv_Order.DataSource = dt;
this.gv_Order.DataBind();
}
protected void gv_Order_RowCreated(object sender, GridViewRowEventArgs e)
{
GridView g = (GridView)sender;
if (g.Rows.Count == 0)
{
return;
}
DropDownList ddl = (DropDownList)g.Rows[g.Rows.Count - 1].FindControl("ddlP");
if (ddl != null)
{
object[] array = new object[] { DateTime.Now.ToString() };
ddl.DataSource = array;
ddl.DataBind();
//自定义初始化操作
DDListM();
}
}
出现问题是:
“ddlP”有一个无效 SelectedValue,因为它不在项目列表中。
参数名: value
protected void btnPr_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
//newRow["PCode"] = "1";
newRow["OrderCode"] = bll.GetNewIDOrder();
newRow["M_IsStock"] = "0";
newRow["M_IsMoney"] = "0";
dt.Rows.Add(newRow);
this.gv_Order.DataSource = dt;
this.gv_Order.DataBind();//就是执行到这里出错的
}
非常着急 ,可以QQ帮我下:274135279或MSN:zlsz_time@hotmail.com
[解决办法]
增加行一般的方法是加工datatable
模板列动态绑定下拉框可以在itemdatabind里面写
回过我的帖子所以过来看下....