【发布时间】:2019-03-26 00:20:39
【问题描述】:
我正在使用 GridView 实现 12 x 31 天的表格。我可以通过单击按钮在单元格上进行选择。我已经用 GridView 实现了它,它没有绘制最后一行,即 12 月行。我还在单元格中使用了硬编码的 html 标记来呈现按钮。但是如果通过控件动态生成也很好?并且如果 row 它必须等于一个月中的天数示例:它将在二月呈现 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 个。Columns 有 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 = ""
};
输出:
【问题讨论】:
-
看起来
if (e.Row.RowType == DataControlRowType.DataRow)块内的代码没有为 DEC 行执行。您需要调试并检查 DEC 行的e.Row.RowType的值是什么... -
comparePrices的长度是多少?看起来comparePrices的长度比预期在 GridView 中显示的行数少一。