在dataTable aspx.net中添加按钮

时间:2015-01-30 08:12:46

标签: asp.net

人们,有人可以告诉我如何在动态创建的数据表中的每一行中添加不同的按钮?我的DataTable绑定到GridView。我需要知道如何在这里添加:drCurrentRow [" Button"] ="每行中的不同按钮&#34 ;;这是我的代码:

<asp:GridView ID="GridView2"  runat="server" OnRowDataBound="GridView2_RowDataBound">
        <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID="btnTest" runat="server" CommandName="odzemi" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
        <asp:Button ID="Button15" Visible="false" runat="server" Text="Button" />
         </ItemTemplate>
    </asp:TemplateField>


            </Columns>
                   </asp:GridView>

//创建dataTable

 private void AddDefaultFirstRecord()
    {

        //creating DataTable  
        DataTable dt = new DataTable();
        DataRow dr;
        dt.TableName = "Markici";
        //creating columns for DataTable  
        dt.Columns.Add(new DataColumn("FirmaID", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("Godina", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("KasaID", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("MarkicaID", typeof(System.Int64)));
        dt.Columns.Add(new DataColumn("Datum", typeof(System.DateTime)));
        dt.Columns.Add(new DataColumn("Masa", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("VrabotenID", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("Artikal", typeof(System.String)));
        dt.Columns.Add(new DataColumn("Cena1", typeof(System.String)));
        dt.Columns.Add(new DataColumn("Kolicina", typeof(System.Decimal)));
        dt.Columns.Add(new DataColumn("Smena", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("VkIznos", typeof(System.Decimal)));
        dt.Columns.Add(new DataColumn("VkDanok", typeof(System.Decimal)));
        dt.Columns.Add(new DataColumn("SysDatum", typeof(System.DateTime)));
        dt.Columns.Add(new DataColumn("Vid", typeof(System.String)));
        dt.Columns.Add(new DataColumn("EdMera", typeof(System.String)));
        dt.Columns.Add(new DataColumn("ArtikalID", typeof(System.String)));
        dt.Columns.Add(new DataColumn("TarifaID", typeof(System.Int16)));
        dt.Columns.Add(new DataColumn("Button", typeof(System.String)));
        dr = dt.NewRow();
        dt.Rows.Add(dr);

        ViewState["Markici"] = dt;

        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

//添加行

   private void AddNewRecordRowToGrid()
    {

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

            if (dtCurrentTable.Rows.Count > 0)
            {

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


                    HttpCookie cookie = Request.Cookies["Democookie"];
                    drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
                    drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
                    drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
                    drCurrentRow["MarkicaID"] = 222;//Request.Cookies["kasa"].Value;
                    drCurrentRow["Datum"] = DateTime.Now;
                    drCurrentRow["Masa"] = Session["masa39"];
                    drCurrentRow["VrabotenID"] = Session["New"];
                    drCurrentRow["Artikal"] = Label3.Text;
                    drCurrentRow["Cena1"] = Label4.Text;
                    drCurrentRow["Kolicina"] = Label5.Text;
                    drCurrentRow["Smena"] = Session["smena1"];
                    drCurrentRow["VkIznos"] = Label6.Text;
                    drCurrentRow["VkDanok"] = Label8.Text;
                    drCurrentRow["SySDatum"] = DateTime.Now;
                    drCurrentRow["Vid"] = Label23.Text;
                    drCurrentRow["EdMera"] = Label10.Text;
                    drCurrentRow["ArtikalID"] = Label9.Text;
                    drCurrentRow["TarifaID"] = Label7.Text;
                    drCurrentRow["Button"] = ;
                }


                //Removing initial blank row  
                if (dtCurrentTable.Rows[0][0].ToString() == "")
                {
                    dtCurrentTable.Rows[0].Delete();
                    dtCurrentTable.AcceptChanges();

                }

                //Added New Record to the DataTable  
                dtCurrentTable.Rows.InsertAt(drCurrentRow, 0);
                //storing DataTable to ViewState  
                ViewState["Markici"] = dtCurrentTable;

                //binding Gridview with New Row  
                GridView2.DataSource = dtCurrentTable;
                GridView2.DataBind();


            }
        }
    }

protected void Button7_Click(object sender, EventArgs e)
    {
    AddNewRecordRowToGrid();
   }


 protected void btnTest_Click(object sender, EventArgs e)
    {
        testPassed = true;
        Session["Counter1"] = newValue;
        Session["Counter"] = newValue;
        if (Session["Markici"] != null || Session["Markici"] != null)
        {

            var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
            var clickedIndex = clickedRow.RowIndex;

            /*decimal*/ old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");

            decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
            decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
            string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
            int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");

           // decimal pocentDanok0 = 0.0m;
          //  decimal pocentDanok1 = 0.18m;              
          //  decimal pocentDanok2 = 0.5m;



            //int oldValue = int.Parse(old); 
           /* decimal*/ newValue = old - 1; //
           Label37.Text = newValue.ToString();

            decimal newIznos = oldIznos - Convert.ToDecimal(Cena1);

            dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString());      
            dtCurrentTable.Rows[clickedIndex].SetField("VkIznos", newIznos.ToString());
            if (TarifaID == 2)
            {
                decimal danok = 0.05m;
                vkdanok1 = Convert.ToDecimal(vkdanok1) + Convert.ToDecimal(Cena1) * Convert.ToDecimal(newValue) * Convert.ToDecimal(danok) / (1 + Convert.ToDecimal(danok));
                vkdanok1 = Math.Truncate(vkdanok1 * 10000m) / 10000m;
                decimal procentDanok =  vkdanok1;
                dtCurrentTable.Rows[clickedIndex].SetField("VkDanok", procentDanok.ToString());
                decimal vkupno = newIznos;
                Label26.Text = vkupno.ToString();
            }
            if (TarifaID == 1)
            {
                decimal danok = 0.018m;
                vkdanok1 = Convert.ToDecimal(vkdanok1) + Convert.ToDecimal(Cena1) * Convert.ToDecimal(newValue) * Convert.ToDecimal(danok) / (1 + Convert.ToDecimal(danok));
                vkdanok1 = Math.Truncate(vkdanok1 * 10000m) / 10000m;
                decimal procentDanok = vkdanok1;
                dtCurrentTable.Rows[clickedIndex].SetField("VkDanok", procentDanok.ToString());
                decimal vkupno = newIznos;
                Label26.Text = vkupno.ToString();
            }
            if (TarifaID == 0)
            {
                decimal danok = 0.0m;
                vkdanok1 = Convert.ToDecimal(vkdanok1) + Convert.ToDecimal(Cena1) * Convert.ToDecimal(newValue) * Convert.ToDecimal(danok) / (1 + Convert.ToDecimal(danok));
                vkdanok1 = Math.Truncate(vkdanok1 * 10000m) / 10000m;
                decimal procentDanok = vkdanok1;
                dtCurrentTable.Rows[clickedIndex].SetField("VkDanok", procentDanok.ToString());
                decimal vkupno = newIznos;
                Label26.Text = vkupno.ToString();
            }

            if (old == 0)
            {
                dtCurrentTable.Rows[clickedIndex].Delete();
                dtCurrentTable.AcceptChanges();

            }
            if (GridView2.Rows.Count == 0)
            {
                Response.Redirect("MasiGradskaKafanak1.aspx");
            }
            GridView2.DataSource = dtCurrentTable;
            GridView2.DataBind();

        }



    }

0 个答案:

没有答案