我是asp.net的新手我最近试图学习如何填充gridview。我已经填充了gridview,但我面临的一个问题是,我绑定到gridview的数据表也显示在屏幕上。我无法弄清楚为什么会这样。请帮忙。
这是我的.cs文件
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
DataTable table = new DataTable();
table.Columns.Add("AmountPrice");
table.Columns.Add("Currency");
table.Rows.Add("12000", "USD");
table.Rows.Add("11000", "Rupees");
table.Rows.Add("12344", "AED");
Session["gridTable"] = table;
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}
protected void add_Click(object sender, EventArgs e)
{
DataTable table1 = (DataTable)Session["gridTable"];
GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
TextBox tbamount= (TextBox)row.FindControl("txtAmount");
TextBox tbcurrency = (TextBox)row.FindControl("txtCurrency");
Button add = (Button)row.FindControl("add");
string vamount = tbamount.Text;
string vcurrency = tbcurrency.Text;
table1.Rows.Add(vamount, vcurrency);
GridView1.DataSource = table1.DefaultView;
GridView1.DataBind();
Session["gridTable"] = table1;
}
这是我的.aspx文件
[![<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AllowPaging="true" DataKeyNames="AmountPrice">
<Columns>
<asp:TemplateField HeaderText="Amount" >
<EditItemTemplate>
<asp:TextBox ID="editamount" runat="server" Text='<%# Eval("AmountPrice") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="amountText" runat="server" Text='<%# Eval("AmountPrice") %>' ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAmount" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Currency">
<EditItemTemplate>
<asp:TextBox ID="editcurrency" runat="server" Text='<%# Eval("Currency") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="currency" runat="server" Text='<%# Eval("Currency") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCurrency" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="add" runat="server" Text="Add" OnClick="add_Click"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>][1]][1]
答案 0 :(得分:0)
AutoGenerateColumns应设置为false。