Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的GridView不添加其他行_C#_Asp.net_Gridview - Fatal编程技术网

C# 我的GridView不添加其他行

C# 我的GridView不添加其他行,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,其中我希望有数据,然后将所有数据添加到数据库中 <asp:BoundField DataField="PurchaseNo" HeaderText="POID" SortExpression="POID" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="ProductID" HeaderText="Pr

我有一个GridView,其中我希望有数据,然后将所有数据添加到数据库中

         <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>

         <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>
它确实添加了如下内容:

         <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>

         <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>
但问题是它不会再添加一行

         <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>
以下是我的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>

品名
价格
量
数量

         <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>


         <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>

这是我的密码

         <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();
    }
public部分类PODetails:System.Web.UI.Page
{
SqlConnection con=新的SqlConnection(Helper.GetCon());
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
添加产品();
}
}
void AddProducts()
{
////con.Open();
////SqlCommand cmd=新的SqlCommand();
//cmd.Connection=con;
//创建数据表
DataTable dt=新的DataTable();
数据行dr;
dt.TableName=“PurchaseDetails”;
//为DataTable创建列
添加(新的数据列(“PurchaseNo”,typeof(int));
添加(新的数据列(“ProductID”,typeof(int));
添加(新数据列(“数量”,类型(int));
添加(新数据列(“名称”,类型(字符串));
添加(新数据列(“价格”,类型(十进制));
dr=dt.NewRow();
dt.Rows.Add(dr);
ViewState[“PurchaseDetails”]=dt;
GridView1.DataSource=dt;
GridView1.DataBind();
}
void AddNewRecordRowToGrid()
{
con.Open();
SqlCommand cmd=新的SqlCommand();
cmd.Connection=con;
如果(ViewState[“PurchaseDetails”]!=null)
{
DataTable dtCurrentTable=(DataTable)ViewState[“PurchaseDetails”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{

对于(int i=1;i您删除了!在页面加载方法中从ispostback中签名,对于循环内部,我做了一些更改..尝试一下

         <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>
 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();
        }
    }
}
void AddNewRecordRowToGrid()
{
con.Open();
SqlCommand cmd=新的SqlCommand();
cmd.Connection=con;
如果(ViewState[“PurchaseDetails”]!=null)
{
DataTable dtCurrentTable=(DataTable)ViewState[“PurchaseDetails”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{

对于(int i=1;i您可以将代码更改为:

         <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>
<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>

您好,很抱歉回复得太晚。它可以工作,但每当我插入另一个数据时,所有数据都会被新数据替换。请帮助您知道如何避免将插入的数据替换为我将插入的另一个数据吗?@Nagib Mahfuzyou每次添加新行时都会删除第一行。如果(dtCurrentTable.Rows[0][0],请删除这些代码
if(dtCurrentTable.Rows[0].ToString()==”{dtCurrentTable.Rows[0]。删除();dtCurrentTable.AcceptChanges();}