如何在 RowDataBound 事件中动态应用 Columnspan?

How to apply Columnspan in RowDataBound event dynamically?

我需要为图中所示的 4 列创建一个列跨度。 这段代码对我不起作用.. 怎么做?提前致谢!!

   if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[3].ColumnSpan = 4; //cells[3] == "Monday"
            e.Row.Cells[3].Text = e.Row.Cells[3].ToString(); 
            e.Row.Cells[7].Text  = "Actual Hours"; //"Actual hours column"
            e.Row.Cells[4].Visible = false; // "Color code column"
            e.Row.Cells[5].Text = "From"; //" From Time column"
            e.Row.Cells[6].Text = "To"; // "To Time column"

        }

您放置的是列而不是行..它可以工作 (此演示适用于 Rowspan)。

void GridView31_RowDataBound(对象发送者, GridViewRowEventArgs e) {

如果( e.Row.RowType == DataControlRowType.DataRow )

{
    if (e.Row.RowIndex % 4 == 0)
    {
        e.Row.Cells[0].Attributes.Add("colspan", "4");
    }
    else
    {
        e.Row.Cells[0].Visible = false;
    }
}

}