【发布时间】:2014-03-08 01:14:09
【问题描述】:
我有 1 个下拉列表和 3 个文本框,我想从文本框中添加数据,单击按钮后它们将被添加到 GridView
<asp:DropdownList ID="ddlClassification" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>Sample 1</asp:ListItem>
<asp:ListItem>Sample 2</asp:ListItem>
<asp:ListItem>Sample 3</asp:ListItem>
</asp:DropdownList>
<asp:TextBox ID="tbCost" runat="server" Width="100" ></asp:TextBox>
<asp:TextBox ID="tbAccumulate" runat="server" Width="100" ></asp:TextBox>
<asp:TextBox ID="tbNRV" runat="server" Width="100" ></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="ADD" onclick="btnAdd_Click" />
这就是我背后的代码
public partial class SetUP : System.Web.UI.Page
{
DataRow dr;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("Classification");
dt.Columns.Add("Cost");
dt.Columns.Add("Accumulate");
dt.Columns.Add("NRV");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
dr = dt.NewRow();
dr["Classification"] = ddlClassification.Text;
dr["Cost"] = tbCost.Text;
dr["Accumulate"] = tbAccumulate.Text;
dr["NRV"] = tbNRV.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
我做过一次,但现在我在GridView1.DataBind(); 上遇到错误,有人可以帮我解决我的问题吗
【问题讨论】:
-
你得到什么错误?
-
DataSource 和 DataSourceID 都在“GridView1”上定义。删除一个定义。
GridView1.DataBind(); -
我觉得您的 gridview 无论如何都会被清除,因为在您的页面加载中,您会在每个帖子后面添加列。你应该包括 if(!isPostBack){ //add columns } t
-
@MatthewWong 现在我在
dr["Classification"] = ddlClassification.Text;上遇到错误