【问题标题】:How to create rows in gridview based on user input?如何根据用户输入在gridview中创建行?
【发布时间】:2020-01-16 13:00:18
【问题描述】:

我想根据用户输入为 gridview 创建行,例如输入月份,然后它将根据用户输入创建 gridview 行。不知道在行添加字段中使用 for 循环是否正确。

   protected void Unnamed_Click(object sender, EventArgs e)
        {
                double Amt, Months;
                Amt = Convert.ToDouble(ILoanAmount);
                Months = Convert.ToDouble(IRepaymentRate);
                calc.Calculator(Amt, Months);
                double Intrest, MonthlyInstallment;
                double Rate = 0.07;
                Intrest = Math.Round(Amt * Months * Rate,2);
                MonthlyInstallment = Math.Round(Amt / ((Math.Pow(1 + Rate, Months) - 1) / (Rate * Math.Pow(1 + Rate, Months))),2);
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Period/Month", typeof(double)),
                            new DataColumn("Periodic Payment", typeof(double)),
                            new DataColumn("Intrest Payment", typeof(double)),
                            new DataColumn("Principal Payment", typeof(double)),
                            new DataColumn("Principal Balance",typeof(double)) });
            dt.Rows.Add(
               for (int i = 0; i < Months; i++)
            {
              //something like here i dont know if its correct if i should use a for loop inside here
            }
                    );
                GridBal.DataSource = dt;
                GridBal.DataBind();
        }

    }```

【问题讨论】:

  • 我会换一种方式。基于 Months 计数的 for 循环,然后在每次迭代中,向 dt 对象添加一个新行。用户输入在哪里?您是否需要循环访问一些控件集以获取数据(即从文本框中)来创建这些行?
  • 你能帮我解决我不明白的代码吗?我是 c# 开发的新手。

标签: c# asp.net gridview rows


【解决方案1】:

这里....我写了一些与您的非常相似的东西,您应该能够使用它来适应您的需要。 row[0] 排到 Period/Month 列,row[1] 排到 Periodic Payment 列,等等...

protected void Page_Load(Object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Period/Month", typeof(double)),
                            new DataColumn("Periodic Payment", typeof(double)),
                            new DataColumn("Intrest Payment", typeof(double)),
                            new DataColumn("Principal Payment", typeof(double)),
                            new DataColumn("Principal Balance",typeof(double)) });

        for (int i = 0; i < 10; i++)
        {
            DataRow row = dt.NewRow();
            row[0] = "1";
            row[1] = "2";
            row[2] = "3";
            row[3] = "4";
            row[4] = "5";
            dt.Rows.Add(row);
        }

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

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2017-09-11
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    相关资源
    最近更新 更多