在c#.NET

时间:2018-04-02 06:05:30

标签: c# asp.net

我非常需要你的帮助。我想为员工薪水生成创建一个网格视图。我正在从后端动态添加列和行。我有一些薪水的额外字段,还有一些用于扣除。我的问题是我想在单独的列中显示其他字段和演绎字段。例如,我在这里添加了一张照片,Example]([![enter image description here

我想动态地完成这一切。这是我现有的代码

 <div class="x_content table-responsive">
<asp:GridView ID="gvSalaryGenerate" runat="server" AutoGenerateColumns="true" class="table table-striped table-bordered" OnRowDataBound="gvSalaryGenerate_RowDataBound">
<Columns>

                                                <asp:TemplateField>
                                                    <HeaderTemplate>
                                                        <tr>
                                                            <th colspan="1">SL No</th>
                                                            <th colspan="1">
                                                                <asp:CheckBox runat="server" ID="CheckBox1" onclick="checkAll(this);" OnCheckedChanged="headerLevelCheckBox_CheckedChanged" /></th>
                                                            <th colspan="1">Held Up Reason</th>
                                                            <th colspan="6" style="text-align: center;">Employee Details</th>
                                                            <th colspan="6" style="text-align: center;">Allowance</th>
                                                            <th colspan="5" style="text-align: center;">Deduction</th>
                                                        </tr>

                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <td>
                                                            <asp:Label ID="lblSRNO" runat="server"
                                                                Text='<%#Container.DataItemIndex+1 %>'></asp:Label></td>

                                                        <td>
                                                            <asp:CheckBox ID="rowLevelCheckBox" runat="server" onclick="Check_Click(this)" />
                                                            <headerstyle verticalalign="Middle" cssclass="Grid_Header" />
                                                            <itemstyle horizontalalign="Left" width="10%" cssclass="Grid_Border" />
                                                            <footerstyle cssclass="Grid_Footer" />
                                                        </td>
                                                        <td>
                                                            <asp:TextBox class="form-control" ID="txtHeldUpReason" runat="server"></asp:TextBox>
                                                        </td>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField Visible="False">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblEmployee_Id" runat="server" Text='<%# Eval("EMPLOYEE_ID")%>' />
                                                    </ItemTemplate>
                                                </asp:TemplateField>

                                                <asp:TemplateField Visible="False">
                                                    <ItemTemplate>
                                                      <asp:Label ID="lblBasicSalary" runat="server" Text='<%# Eval("BASIC_SALARY")%>' />
                                                 </ItemTemplate>
                                          </asp:TemplateField>
                                       </Columns>
                              </asp:GridView>
                            </div>

可以在c#中动态添加复选框和文本框字段吗?怎么样?请帮我。

1 个答案:

答案 0 :(得分:0)

 protected void btnGenerate_Click(object sender, EventArgs e)
{
    try
    {
        if (GenerateValidation())
        {
            Hashtable ht = new Hashtable();
            ht.Add("SearchCriteria", GetSearchCriteria());
            DataTable Data = new DataTable();

            DataSet ds = new DataSet();
            ds = obj_DBUtility.GetDataSetByProcedure(ht, "PR_GET_EMPLOYEE_DETAILS_FOR_SALARY_GENERATE");
            Data = ds.Tables[0];

            DataTable dtData = salaryItemsService.GetAllPRSalaryItemsSrtForSalaryGenerate();

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                Data.Columns.Add(dtData.Rows[i]["ITEM_NAME"].ToString());
            }
            Data.Columns.Add("Net Salary");

            gvSalaryGenerate.HeaderRow.Cells.Add("SL No", "Empoloyee Details", "Allowance", "Deduction");
            gvSalaryGenerate.DataSource = Data;
            gvSalaryGenerate.DataBind();

            DataLoadInGrid(dtData);
            SetButtonVisible();
        }
    }
    catch (Exception ex)
    {
        ReallySimpleLog.WriteLog(ex);
    }
}

这是在后端编写的用于在网格中添加行的内容,也是为了动态添加行值

 private void DataLoadInGrid(DataTable dtData)
{
    int count = gvSalaryGenerate.Rows.Count;
    foreach (GridViewRow gr in gvSalaryGenerate.Rows)
    {
        double addition = 0;
        double deduction = 0;
        Label lblEmpId = ((Label)gr.FindControl("lblEmployee_Id"));
        long empId = Convert.ToInt64(lblEmpId.Text);

        //Get Basic Salary
        Label bs = ((Label)gr.FindControl("lblBasicSalary"));
        double basicSalary = Convert.ToInt64(bs.Text);

        DataTable dtEmpData = empSalaryItemService.GetrEmployeeSalaryItemForSalaryGenerate(empId);

        for (int i = 0; i < dtData.Rows.Count; i++)
        {
            //Load Salary Item Data

            foreach (TableCell tc_ in (gvSalaryGenerate).HeaderRow.Cells)
            {
                if (tc_.Text.Equals(dtData.Rows[i]["ITEM_NAME"].ToString()))
                {
                    int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc_);

                    if (dtData.Rows[i]["IS_PERSONAL"].ToString() != "True")
                    {
                        if (dtData.Rows[i]["IS_PERCENT"].ToString() == "True")
                        {
                            int itemValuePrcnt = Convert.ToInt32(dtData.Rows[i]["ITEM_VALUE"].ToString());
                            double itemValue = Convert.ToDouble((basicSalary * itemValuePrcnt / 100));
                            gr.Cells[index].Text = itemValue.ToString();
                            if ((dtData.Rows[i]["ITEM_TYPE"].ToString()) == "A")
                            {
                                addition = addition +itemValue;
                            }
                            else
                            {
                                deduction = deduction + itemValue;
                            }
                        }
                        else
                        {
                            gr.Cells[index].Text = dtData.Rows[i]["ITEM_VALUE"].ToString();

                            if ((dtData.Rows[i]["ITEM_TYPE"].ToString()) == "A")
                            {
                                addition = addition + Convert.ToInt64(dtData.Rows[i]["ITEM_VALUE"].ToString());
                            }
                            else
                            {
                                deduction = deduction + Convert.ToInt64(dtData.Rows[i]["ITEM_VALUE"].ToString());
                            }
                        }
                    }
                    else
                    {
                        //Load Salary Item Employee Data                   
                        if (dtEmpData.Rows.Count > 0)
                        {
                            for (int j = 0; j < dtEmpData.Rows.Count; j++)
                            {
                                if (empId == Convert.ToInt64(dtEmpData.Rows[j]["EMPLOYEE_ID"]))
                                {
                                    if (dtEmpData.Rows[j]["ITEM_NAME"].ToString() == dtData.Rows[i]["ITEM_NAME"].ToString())
                                    {
                                        foreach (TableCell tc in (gvSalaryGenerate).HeaderRow.Cells)
                                        {
                                            if (tc.Text.Equals(dtEmpData.Rows[j]["ITEM_NAME"].ToString()))
                                            {
                                                index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                                                if (dtEmpData.Rows[j]["IS_PERCENT"].ToString() == "True")
                                                {
                                                    int itemValuePrcnt = Convert.ToInt32(dtEmpData.Rows[j]["ITEM_VALUE"].ToString());
                                                    double itemValue = Convert.ToDouble((basicSalary * itemValuePrcnt / 100));
                                                    gr.Cells[index].Text = itemValue.ToString();
                                                    if ((dtData.Rows[i]["ITEM_TYPE"].ToString()) == "A")
                                                    {
                                                        addition = addition + itemValue;
                                                    }
                                                    else
                                                    {
                                                        deduction = deduction + itemValue;
                                                    }
                                                }
                                                else
                                                {
                                                    gr.Cells[index].Text = dtEmpData.Rows[j]["ITEM_VALUE"].ToString();
                                                    if ((dtData.Rows[i]["ITEM_TYPE"].ToString()) == "A")
                                                    {
                                                        addition = addition + Convert.ToInt64(dtData.Rows[i]["ITEM_VALUE"].ToString());
                                                    }
                                                    else
                                                    {
                                                        deduction = deduction + Convert.ToInt64(dtData.Rows[i]["ITEM_VALUE"].ToString());
                                                    }
                                                }
                                            }
                                        }

                                    }
                                }
                            }

                        }
                    }

                }
            }
        }

        foreach (TableCell tc in (gvSalaryGenerate).HeaderRow.Cells)
        {
            if (tc.Text.Equals("EMPLOYEE_NAME"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Employee";
            }
            if (tc.Text.Equals("EMPLOYEE_CODE"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Employee Code";
            }
            if (tc.Text.Equals("CENTER_NAME"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Center";
            }
            if (tc.Text.Equals("DEPARTMENT_NAME"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Department";
            }
            if (tc.Text.Equals("DESIGNATION_NAME"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Designation";
            }
            if (tc.Text.Equals("BASIC_SALARY"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Basic Salary";
            }
            if (tc.Text.Equals("CENTER_ID"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Visible = false;
                gr.Cells[index].Visible = false;
            }
            if (tc.Text.Equals("DEPARTMENT_ID"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Visible = false;
                gr.Cells[index].Visible = false;
            }
            if (tc.Text.Equals("EMPLOYEE_ID"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Visible = false;
                gr.Cells[index].Visible = false;
            }
            if (tc.Text.Equals("b1"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "";
            }
            if (tc.Text.Equals("b2"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "SL No";
            }
            if (tc.Text.Equals("b3"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "";
            }
            if (tc.Text.Equals("b4"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gvSalaryGenerate.HeaderRow.Cells[index].Text = "Held Up Reason";
            }
            if (tc.Text.Equals("Net Salary"))
            {
                int index = (gvSalaryGenerate).HeaderRow.Cells.GetCellIndex(tc);
                gr.Cells[index].Text = Convert.ToString(basicSalary+addition-deduction);
            }
        }

    }
}