使用新内容将行添加到gridview [英] Add Rows to gridview with new content
问题描述
您好,
我有一个下拉列表和一个带有名为add的按钮的文本框。
功能应该是这样的:
从下拉列表中选择某个值后,在添加按钮时在文本框中写入内容按下它将填充下面网格视图中字段的值。
当从下拉列表中选择第二个值时,文本框中会显示一些文本,按钮添加应该在网格中添加一个新行与前一个。
按钮按下时新行将继续添加。
请帮助。
Hello,
I have a drop-down list and a text box with a button named add.
The functionality should be something like this:
After selecting a certain value from the drop-down, writing something in the text box when the Add button is pressed it will populate the values from the fields in a grid view below.
When a second value is selected from the drop-down with some text from the text box, the button add should append a new row in the grid with the previous one.
And new rows will keep on adding on button press.
Please help.
推荐答案
按钮添加代码:
Button add code :
protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
try
{
AddToOrder();
}
catch
{
string Msg = "<script>alert('Error occurred during adding the product..!');</script>";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
}
}
public void AddToOrder()
{
OrdersDataContext context = DataContextSingleton.Instance;
_order = new Order();
_order.quantity= Convert.ToInt32(dropdownlist.SelectedItem.Value);
_order.price = Convert.ToInt32(dropdownlist2.SelectedItem.Value);
_order.note = TextBox4.Text;
context.Orders.InsertOnSubmit(order);
context.SubmitChanges();
this.RefreshGrid();
}
public void RefreshGrid()
{
OrdersDataContext contex = DataContextSingleton.Instance;
var query = (from o in _context.Orders
select new
{
Quantity = o.quant,
Price = o.price,
notes= o.note
});
OrderGridView.DataSource = query.ToList();
OrderGridView.DataBind();
}
我希望这可以帮到你!
I hope this could help you!
您可以使用DataTable和Session来实现此目的。以下是供参考的代码段
You can use DataTable along with Session to achieve this. Below is code snippet for reference
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
<asp:listitem>Item1</asp:listitem>
<asp:listitem>Item2</asp:listitem>
<asp:listitem>Item3</asp:listitem>
</asp:dropdownlist>
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown" />
</div>
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
</asp:gridview>
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = null;
if (Session["datatable"] != null)
{
dt = (DataTable)Session["datatable"];
}
else
{
dt = GetDataTable();
}
DataRow dr = dt.NewRow();
dr["Items"] = DropDownList1.SelectedValue;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["datatable"] = dt;
}
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Items"));
return dt;
}
这篇关于使用新内容将行添加到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!