我在数据库中有一个包含多列的表,但是我只想将ItemId和Price提取到我的GridView表中。
这是用户流程:用户将在网络表单的文本框中输入ItemId和Qty。该ItemId将在数据库中搜索。找到ItemId后,将该ItemId和相应的Price以及用户的“数量”输入绑定到GridView中。
下面的代码中是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridViewData();
GridView1.DataSource = dtPO;
GridView1.DataBind();
}
}
private void gridViewData()
{
dtPO.Columns.Add("ItemId", typeof(string));
dtPO.Columns.Add("Description", typeof(string));
dtPO.Columns.Add("QtyReorder", typeof(int));
dtPO.Columns.Add("Price", typeof(decimal));
dtPO.Columns.Add("Amount", typeof(decimal));
Session["dtPOInSession"] = dtPO;
}
protected void AddItemButton_Click(object sender, EventArgs e)
{
LastADEntities1 context = new LastADEntities1();
ItemList item = new ItemList();
var itemDescription = context.ItemLists.Where(x => x.ItemId == ItemIdTextBox.Text).Select(x => x.Description);
var itemPrice = context.ItemLists.Where(x => x.ItemId == ItemIdTextBox.Text.ToString()).Select(x => x.Price).ToList();
DataRow dr = dtPO.NewRow();
dr["ItemId"] = ItemIdTextBox.Text;
dr["Description"] = itemDescription;
dr["QtyReorder"] = Convert.ToInt32(QtyReorderTextBox.Text);
dr["Price"] = itemPrice;
dr["Amount"] = Convert.ToInt32(itemPrice) * Convert.ToInt32(QtyReorderTextBox.Text);
dtPO.Rows.Add(dr);
GridView1.DataBind();
}
}
我正在使用C#和实体模型.edmx
下面是我的部分网络表单代码
“ SelectCommand =” SELECT * FROM [供应商]“>
<asp:GridView ID="GridView1" runat="server" Height="157px" Width="736px" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="Item ID" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="QtyReorder" HeaderText="Quantity" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
我有一个System.ArgumentException:'列'ItemId'不属于该表。'
请帮助提供有关修复的指示或建议。谢谢!
答案 0 :(得分:0)
而不是在AddButton方法中使用DataBind(),而是制作另一个函数来填充gridview数据,如bind gridview,并像这样在(!isPostBack)的else语句中调用它
if(!isPostBack)
{
//add columns to gridview
........................
........................
}
else
{
fillData();
............
............
//OR try to DataBind() here and fill through addButton Method
}
我希望这样可以正常工作,未经测试。