我有一个GridView,我希望在那里有数据,然后将所有数据添加到数据库中。
Gridview is before my SAVE Products BTN; textboxes are before ADD Product 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();
}
我刚试过添加新行的代码。它只是没有工作
我不知道我的代码有什么问题。提前谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
在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();
}
}
}
如果不起作用,请告诉我。谢谢。
答案 2 :(得分:0)
您可以将代码更改为:
<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();
}
}