Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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_.net_Sql Server_Gridview - Fatal编程技术网

C# 如何将可编辑的GridView值存储到数据库

C# 如何将可编辑的GridView值存储到数据库,c#,asp.net,.net,sql-server,gridview,C#,Asp.net,.net,Sql Server,Gridview,这里我使用GridView控件..我的输出屏幕如下。。 在这里,当我单击submit按钮时,如何计算总数以及如何将这些数据存储到数据库 我的密码在这里 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("S.No", typeo

这里我使用GridView控件..我的输出屏幕如下。。

在这里,当我单击submit按钮时,如何计算总数以及如何将这些数据存储到数据库

我的密码在这里

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("S.No", typeof(string)));
        dt.Columns.Add(new DataColumn("Article No", typeof(string)));
        dt.Columns.Add(new DataColumn("Item Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Order Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("UOM", typeof(string)));
        dt.Columns.Add(new DataColumn("Rate", typeof(string)));
        dt.Columns.Add(new DataColumn("Currency", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));
        DataRow dr = dt.NewRow();
        dr["S.No"] = 1;
        dr["Article No"] = string.Empty;
        dr["Item Description"] = string.Empty;
        dr["Order Quantity"] = 0;
        dr["UOM"] = string.Empty;
        dr["Rate"] = 0;
        dr["Currency"] = string.Empty;
        dr["Amount"] = 0;
        dt.Rows.Add(dr);
        ViewState["currenttable"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();


        DropDownList box2 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("DropDownList1");
        box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
        box2.DataTextField = "Port_Number";
        box2.DataValueField = "Description";
        box2.DataBind();
        box2.Items.Insert(0, " ");


        DropDownList box12 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList3");
        box12.DataSource = SIMBL.ShowSimpleUnitItemInfo();
        box12.DataTextField = "Name";
        box12.DataValueField = "Symbol";
        box12.DataBind();
        box12.Items.Insert(0, " ");

        DropDownList box13 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList4");
        box13.DataSource = SIMBL.ShowSimpleUnitItemInfo();
        box13.DataTextField = "Name";
        box13.DataValueField = "Symbol";
        box13.DataBind();
        box13.Items.Insert(0, " ");

        DropDownList box14 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList5");
        box14.DataSource = SIMBL.ShowSimpleUnitItemInfo();
        box14.DataTextField = "Name";
        box14.DataValueField = "Symbol";
        box14.DataBind();
        box14.Items.Insert(0, " ");
    }

}


public void addnewrow()
{
    int rowIndex = 0;

    if (ViewState["currenttable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["currenttable"];
        DataRow drCurrentRow = null;



        decimal a = 0.00M;

        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
        {
            //extract the TextBox values
            TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
            DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
            TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
            TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
            TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
            TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
            Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
            TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
            TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
            TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
            Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
            DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
            DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
            DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
            Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
            TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
            TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
            TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
            Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
            DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
            DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
            DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
            Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
            TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
            TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
            TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");

            TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");

            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["S.No"] = i + 1;


            dtCurrentTable.Rows[i - 1]["Article No"] = box2.Text;

            dtCurrentTable.Rows[i - 1]["Item Description"] = box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text;

            dtCurrentTable.Rows[i - 1]["Order Quantity"] = box8.Text + "," + box9.Text + "," + box10.Text;

            dtCurrentTable.Rows[i - 1]["UOM"] = box12.Text + "," + box13.Text + "," + box14.Text;

            dtCurrentTable.Rows[i - 1]["Rate"] = box16.Text + "," + box17.Text + "," + box18.Text;

            dtCurrentTable.Rows[i - 1]["Currency"] = box20.Text + "," + box21.Text + "," + box22.Text;

            dtCurrentTable.Rows[i - 1]["Amount"] = box24.Text + "," + box25.Text + "," + box26.Text;




            a = a + Convert.ToDecimal(Convert.ToString(box24.Text));
            a = a + Convert.ToDecimal(Convert.ToString(box25.Text));
            a = a + Convert.ToDecimal(Convert.ToString(box26.Text));


            box27.Text = Convert.ToString(a);



            rowIndex++;
        }
        dtCurrentTable.Rows.Add(drCurrentRow);
        ViewState["currenttable"] = dtCurrentTable;

        GridView1.DataSource = dtCurrentTable;
        GridView1.DataBind();




        for (int i = 0; i <= dtCurrentTable.Rows.Count - 1; i++)
        {
            DropDownList box2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1");
            box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
            box2.DataTextField = "Port_Number";
            box2.DataValueField = "Description";
            box2.DataBind();
            box2.Items.Insert(0, " ");

            DropDownList box12 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList3");
            box12.DataSource = SIMBL.ShowSimpleUnitItemInfo();
            box12.DataTextField = "Name";
            box12.DataValueField = "Symbol";
            box12.DataBind();
            box12.Items.Insert(0, " ");

            DropDownList box13 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList4");
            box13.DataSource = SIMBL.ShowSimpleUnitItemInfo();
            box13.DataTextField = "Name";
            box13.DataValueField = "Symbol";
            box13.DataBind();
            box13.Items.Insert(0, " ");

            DropDownList box14 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList5");
            box14.DataSource = SIMBL.ShowSimpleUnitItemInfo();
            box14.DataTextField = "Name";
            box14.DataValueField = "Symbol";
            box14.DataBind();
            box14.Items.Insert(0, " ");

            }


        }
    SetPreviousData();
}
private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["currenttable"] != null)
    {
        DataTable dt = (DataTable)ViewState["currenttable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
                DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
                TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
                TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
                TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
                Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
                TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
                TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
                TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
                Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
                DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
                DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
                DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
                Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
                TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
                TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
                TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
                Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
                DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
                DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
                DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
                Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
                TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
                TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
                TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");

                TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");

                box1.Text = dt.Rows[i]["S.No"].ToString();
                box2.Text = dt.Rows[i]["Article No"].ToString();

                if (Convert.ToString(dt.Rows[i]["Item Description"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["Item Description"]).Trim().Split(',');
                    box3.Text = strDesc[0].ToString();
                    box4.Text = strDesc[1].ToString();
                    box5.Text = strDesc[2].ToString();
                    box6.Text = strDesc[3].ToString();
                }

                if (Convert.ToString(dt.Rows[i]["Order Quantity"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["Order Quantity"]).Trim().Split(',');
                    box8.Text = strDesc[0].ToString();
                    box9.Text = strDesc[1].ToString();
                    box10.Text = strDesc[2].ToString();
                }

                if (Convert.ToString(dt.Rows[i]["UOM"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["UOM"]).Trim().Split(',');
                    box12.Text = strDesc[0].ToString();
                    box13.Text = strDesc[1].ToString();
                    box14.Text = strDesc[2].ToString();
                }

                if (Convert.ToString(dt.Rows[i]["Rate"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["Rate"]).Trim().Split(',');
                    box16.Text = strDesc[0].ToString();
                    box17.Text = strDesc[1].ToString();
                    box18.Text = strDesc[2].ToString();
                }

                if (Convert.ToString(dt.Rows[i]["Currency"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["Currency"]).Trim().Split(',');
                    box20.Text = strDesc[0].ToString();
                    box21.Text = strDesc[1].ToString();
                    box22.Text = strDesc[2].ToString();
                }

                if (Convert.ToString(dt.Rows[i]["Amount"]).Trim() != "")
                {
                    string[] strDesc = Convert.ToString(dt.Rows[i]["Amount"]).Trim().Split(',');
                    box24.Text = strDesc[0].ToString();
                    box25.Text = strDesc[1].ToString();
                    box26.Text = strDesc[2].ToString();

                }



                rowIndex++;

            }
        }
    }
}

//DropDownList Selection Change..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

        for (int i = 0; i <= dt.Rows.Count - 1; i++)
        {
            TextBox box3 = (TextBox)GridView1.Rows[i].Cells[2].FindControl("TextBox8");
            DropDownList box2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1");
            box3.Text = box2.SelectedItem.Value;
        }

}

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box12 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList3");
        box12.Text = box12.SelectedItem.Value;
    }
}

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box13 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList4");
        box13.Text = box13.SelectedItem.Value;
    }
}

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box14 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList5");
        box14.Text = box14.SelectedItem.Value;
    }
}



protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box20 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList7");
        box20.Text = box20.SelectedItem.Value;

        TextBox box24 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox21");
        TextBox box16 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox17");
        TextBox box8 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox13");

        box24.Text = (Convert.ToDecimal(box16.Text) * Convert.ToDecimal(box8.Text)).ToString();

    }

}


protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box21 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList8");
        box21.Text = box21.SelectedItem.Value;

        TextBox box25 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox22");
        TextBox box17 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox18");
        TextBox box9 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox14");
        box25.Text = (Convert.ToDecimal(box17.Text) * Convert.ToDecimal(box9.Text)).ToString();
    }
}

protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["currenttable"];

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        DropDownList box22 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList9");
        box22.Text = box22.SelectedItem.Value;

        TextBox box26 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox23");
        TextBox box18 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox19");
        TextBox box10 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox15");
        box26.Text = (Convert.ToDecimal(box18.Text) * Convert.ToDecimal(box10.Text)).ToString();
    }
}


//For Button Click...
protected void Button1_Click1(object sender, EventArgs e)
{
    addnewrow();

}
}

提前谢谢。

Mathi

在提交时调用addnewrow函数以更新dtCurrentTable,然后使用dtCurrentTable.Rows.Count-1的for循环可以逐个更新数据库中的所有记录

For循环如下所示

for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
每个循环都可以计算出总数,最后可以将其存储到数据库中

要在addnewrow函数中获取代码下面的总do

    decimal totalAmt = 0.00M;

    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
    {
        .
        .
        .
        .
        .

        ///////// -----> end of the for loop

      if(box24.text.trim() == "")
          box24.text = "0";
      if(box25.text.trim() == "")
          box25.text = "0";
      if(box26.text.trim() == "")
          box26.text = "0";

        totalAmt = totalAmt + Convert.ToDecimal(Convert.ToString(box24.Text));
        totalAmt = totalAmt + Convert.ToDecimal(Convert.ToString(box25.Text));            
        totalAmt = totalAmt + Convert.ToDecimal(Convert.ToString(box26.Text));
    }

    TextBox box30 = (TextBox)GridView1.FooterRow.FindControl("Total Textbox ID");
    box30.Text = Convert.ToString(totalAmt);

你能分享aspx和后面的代码吗当然可以@SameieyMehdi..但不完整的代码是无用的..完成这段代码后我会分享。在这里添加一些代码..没有代码就没有帮助@Mathi在提交时调用addnewrow函数以更新dtCurrentTable,然后使用dtCurrentTable.Rows.Count的for循环可以逐个更新数据库中的所有记录。每个循环都可以计算总数,最后可以将其存储到数据库中。@Mathi您需要跳过SetPreviousData;函数在提交时调用addnewrow…抱歉@VishalPatel。。我的问题没有什么错误。。单击“添加”按钮时,需要以循环方式计算总计,然后单击“提交”按钮时,需要将所有值存储到数据库中。@Mathi您可以在addnewrow函数中计算总计。取一个变量,输入int i=1;我@Mathi对你没意见还是还有疑问?你的解释很好@VishalPatel。。但是我不知道怎么开始。。如果你不是我的,你能给我一个总的编码吗…@Mathi这个答案对你有用。。。?