如何在每行GridView单元内绘制按钮?

时间:2019-03-26 00:20:39

标签: c# asp.net gridview

我正在使用GridView实现12 x 31天的表格。我可以通过单击按钮在单元格上进行选择。我已经用某种方式实现了GridView的问题,它没有绘制最后一行,即12月行。我还在单元格中使用了硬编码的html标签来呈现按钮。但是,如果由控件动态生成,这也很好吗?同样,如果行必须等于一个月的某天,例如:它将在2月呈现28个按钮。

单击此处将当前数据加载到GridView中。

protected void ButtonFilter_Click(object sender, EventArgs e)
        {
                               DataTable dataTable = new DataTable();

                    dataTable.Columns.AddRange(new DataColumn[32]
                    {
                        new DataColumn("Month"),
                        new DataColumn("Day1"),
                        new DataColumn("Day2"),
                        new DataColumn("Day3"),
                        new DataColumn("Day4"),
                        new DataColumn("Day5"),
                        new DataColumn("Day6"),
                        new DataColumn("Day7"),
                        new DataColumn("Day8"),
                        new DataColumn("Day9"),
                        new DataColumn("Day10"),
                        new DataColumn("Day11"),
                        new DataColumn("Day12"),
                        new DataColumn("Day13"),
                        new DataColumn("Day14"),
                        new DataColumn("Day15"),
                        new DataColumn("Day16"),
                        new DataColumn("Day17"),
                        new DataColumn("Day18"),
                        new DataColumn("Day19"),
                        new DataColumn("Day20"),
                        new DataColumn("Day21"),
                        new DataColumn("Day22"),
                        new DataColumn("Day23"),
                        new DataColumn("Day24"),
                        new DataColumn("Day25"),
                        new DataColumn("Day26"),
                        new DataColumn("Day27"),
                        new DataColumn("Day28"),
                        new DataColumn("Day29"),
                        new DataColumn("Day30"),
                        new DataColumn("Day31")
                    });

                    DataRow dataRow;

                    var months = new string[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

                    var button = new Button
                    {
                        ID = "ButtonCell",
                        Text = ""
                    };

                    for (int i = 0; i < comparePrices.Count; i++)
                    {
                        dataRow = dataTable.NewRow();

                        dataRow["Month"] = months[i];

                        for (int j = 0; j < comparePrices[i].Pricings.Count; j++)
                        {
                            // Load the here.
                            //dataRow[string.Format("Day{0}", j + 1)] = comparePrices[i].Pricings[j].Price;                            
                        }

                        dataTable.Rows.Add(dataRow);
                    }

                    ListOfComparePrice = comparePrices;

                    GridViewPricing.DataSource = dataTable;
                    GridViewPricing.DataBind();
                }

这是RowDataBound后端代码。 GridViewPricing.Rows.Count有12个。列有32个计数。

protected void GridViewPricing_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (GridViewRow row in GridViewPricing.Rows)
                {
                    for (int i = 0; i < GridViewPricing.Columns.Count; i++)
                    {
                        if (i != 0)
                        {
                            row.Cells[i].Text = string.Format("<div><input type='button' style='border: 0; display:block; padding:4px; width:100%; height:100%;' id={0}/></div>", "hi");
                        }
                    }
                }
            }
        }

也可以代替硬编码的html。我想在后端使用动态代码来生成按钮。

 var button = new Button
                    {
                        ID = "ButtonCell",
                        Text = ""
                    };

输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

问题是您有一个嵌套循环。在GridViewPricing_RowDataBound内,有以下一行

foreach (GridViewRow row in GridViewPricing.Rows)

但是已经为每一行触发了RowDataBound事件,因此每次您再次循环前几行时,都会触发该事件。但是当前行仅在RowDataBound事件之后添加到GridView中。这就是为什么您缺少最后一行。应该是

protected void GridViewPricing_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < GridViewPricing.Columns.Count; i++)
        {
            if (i != 0)
            {
                row.Cells[i].Text = string.Format("<div><input type='button' style='border: 0; display:block; padding:4px; width:100%; height:100%;' id={0}/></div>", "hi");
            }
        }
    }
}