我的网格视图不会添加另一行
本文关键字:一行 添加 网格 视图 我的 | 更新日期: 2023-09-27 18:32:51
>我有一个GridView,我想在那里有数据,然后将所有数据添加到数据库中。
Gridview 在我的 SAVE 产品 BTN 之前;文本框位于 ADD 产品 BTN 之前
它确实像这样添加:
见图片
但问题是它不会再添加一行....
这是我的 ASP.NET 代码:
<table >
<tr>
<td class="style2">
Product Name
</td>
<td class="style1">
Price
</td>
<td>
Quantity
</td>
<td>
Amount
</td>
</tr>
<tr>
<td class="style2">
<asp:SqlDataSource ID="Products" runat="server"
ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>"
SelectCommand="SELECT [Name] FROM [Products]" ></asp:SqlDataSource>
</td>
<asp:DropDownList ID="Name" runat="server" type="Number" DataSourceID="Products"
DataTextField="Name" DataValueField="Name" Width="83px" ></asp:DropDownList>
</td>
<td class="style1">
<asp:TextBox ID="Price" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Quantity" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Amount" runat="server"></asp:TextBox >
</td>
</tr>
<tr>
<td class="style3"></td>
<td class="style2"></td>
<td>
</td>
<td>
<asp:Button ID="AddProduct" runat="server" Text="Add Product"
BackColor="#999966" onclick="AddProduct_Click" /></td>
</tr>
<asp:BoundField DataField="PurchaseNo" HeaderText="POID"
SortExpression="POID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
</Columns>
</asp:GridView>
<asp:Button ID="btnsubmitProducts" runat="server" style="color:White"
Text="Save Products" BackColor="#999966"
onclick="btnsubmitProducts_Click" />
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
这是我的代码隐藏
public partial class PODetails : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Helper.GetCon());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddProducts();
}
}
void AddProducts()
{
////con.Open();
////SqlCommand cmd = new SqlCommand();
//cmd.Connection = con;
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "PurchaseDetails";
//creating columns for DataTable
dt.Columns.Add(new DataColumn("PurchaseNo", typeof(int)));
dt.Columns.Add(new DataColumn("ProductID", typeof(int)));
dt.Columns.Add(new DataColumn("Quantity", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Price", typeof(decimal)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["PurchaseDetails"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
void AddNewRecordRowToGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (ViewState["PurchaseDetails"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["PurchaseDetails"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Name"] = Name.Text;
drCurrentRow["Quantity"] = Convert.ToInt32(Quantity.Text);
drCurrentRow["Price"] = Convert.ToDecimal(Price.Text);
//drCurrentRow["Price"] = Convert.ToDecimal(Price.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["PurchaseDetails"] = dtCurrentTable;
//binding Gridview with New Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
void BulkInsertToDataBase()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
DataTable dtPurchaseDetails = (DataTable)ViewState["PurchaseDetails"];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(con);
//assigning Destination table name
objbulk.DestinationTableName = "PurchaseDetails";
//Mapping Table column
objbulk.ColumnMappings.Add("Name", "Name");
objbulk.ColumnMappings.Add("Quantity", "Quantity");
objbulk.ColumnMappings.Add("Price", "Price");
//inserting bulk Records into DataBase
objbulk.WriteToServer(dtPurchaseDetails);
}
protected void AddProduct_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
protected void btnsubmitProducts_Click(object sender, EventArgs e)
{
BulkInsertToDataBase();
}
我刚刚尝试编写添加新行的代码。 它只是不起作用
我不知道我的代码出了什么问题。提前感谢!
在 for 循环中,我做了一些更改。试试看
void AddNewRecordRowToGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (ViewState["PurchaseDetails"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["PurchaseDetails"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Name"] = Name.Text;
dtCurrentTable.Rows[i - 1]["Quantity"] = Convert.ToInt32(Quantity.Text);
dtCurrentTable.Rows[i - 1]["Price"] = Convert.ToDecimal(Price.Text);
//drCurrentRow["Price"] = Convert.ToDecimal(Price.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["PurchaseDetails"] = dtCurrentTable;
//binding Gridview with New Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
如果它不起作用,请告诉我。谢谢。
你删除了 ! Page_Load方法中的回邮符号
您可以将代码更改为:
<table >
<tr>
<td class="style2">
Product Name
</td>
<td class="style1">
Price
</td>
<td>
Quantity
</td>
<td>
Amount
</td>
</tr>
<tr>
<td class="style2">
<asp:SqlDataSource ID="Products" runat="server"
ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>"
SelectCommand="SELECT [Name] FROM [Products]" ></asp:SqlDataSource>
</td>
<asp:DropDownList ID="Name" runat="server" type="Number" DataSourceID="Products"
DataTextField="Name" DataValueField="Name" Width="83px" ></asp:DropDownList>
</td>
<td class="style1">
<asp:TextBox ID="Price" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Quantity" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Amount" runat="server"></asp:TextBox >
</td>
</tr>
<tr>
<td class="style3"></td>
<td class="style2"></td>
<td>
</td>
<td>
<asp:Button ID="AddProduct" runat="server" Text="Add Product"
BackColor="#999966" onclick="AddProduct_Click" /></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField DataField="PurchaseNo" HeaderText="POID"
SortExpression="POID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
</Columns>
</asp:GridView>
和你的代码隐藏:
public partial class PODetails : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Helper.GetCon());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddProducts();
}
}
void AddProducts()
{
DataTable dt = new DataTable();
dt.TableName = "PurchaseDetails";
//creating columns for DataTable
dt.Columns.Add(new DataColumn("PurchaseNo", typeof(int)));
dt.Columns.Add(new DataColumn("ProductID", typeof(int)));
dt.Columns.Add(new DataColumn("Quantity", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Price", typeof(decimal)));
ViewState["PurchaseDetails"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
void AddNewRecordRowToGrid()
{
if (ViewState["PurchaseDetails"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["PurchaseDetails"];
DataRow drCurrentRow = null;
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Name"] = Name.Text;
drCurrentRow["Quantity"] = Convert.ToInt32(Quantity.Text);
drCurrentRow["Price"] = Convert.ToDecimal(Price.Text);
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["PurchaseDetails"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
void BulkInsertToDataBase()
{
}
protected void AddProduct_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
protected void btnsubmitProducts_Click(object sender, EventArgs e)
{
BulkInsertToDataBase();
}
}