WEB的GRIDVIEW的行合并及列合并,这次做的这个思路是较为清晰可行的。
推荐给大家。
protected void GridView4_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#D6E0F2'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
if (e.Row.DataItem != null)
{
if (string.IsNullOrEmpty((e.Row.DataItem as DataRowView)["Content2"].ToString().Trim()))
{
e.Row.Cells[3].ColumnSpan = 2;
e.Row.Cells[4].Visible = false;
}
if (e.Row.DataItemIndex > 0)
{
//范围 RowSpan
string curRowValue = (e.Row.DataItem as DataRowView)["PriceArea"].ToString().Trim();
string preRowValue = "";
preRowValue = (GridView4.Rows[e.Row.RowIndex - 1].FindControl("Label2") as Label).Text.Trim();
if (preRowValue.Equals(curRowValue))
{
GridViewRow gvr = e.Row;
gvr.Cells[2].Visible = false;
for (int ci = e.Row.RowIndex - 1; ci >= 0; ci--)
{
if (GridView4.Rows[ci].Cells[2].Visible)
{
if (GridView4.Rows[ci].Cells[2].RowSpan == 0)
GridView4.Rows[ci].Cells[2].RowSpan = 2;
else
GridView4.Rows[ci].Cells[2].RowSpan++;
break;
}
}
}
else
{
e.Row.Cells[2].Visible = true;
}
//类别1 RowSpan
string curRowValue2 = (e.Row.DataItem as DataRowView)["Content1"].ToString().Trim();
string preRowValue2 = "";
preRowValue2 = (GridView4.Rows[e.Row.RowIndex - 1].FindControl("class1") as Label).Text.Trim();
if (preRowValue2.Equals(curRowValue2))
{
GridViewRow gvr = e.Row;
gvr.Cells[3].Visible = false;
for (int ci = e.Row.RowIndex - 1; ci >= 0; ci--)
{
if (GridView4.Rows[ci].Cells[3].Visible)
{
if (GridView4.Rows[ci].Cells[3].RowSpan == 0)
GridView4.Rows[ci].Cells[3].RowSpan = 2;
else
GridView4.Rows[ci].Cells[3].RowSpan++;
break;
}
}
}
else
{
e.Row.Cells[3].Visible = true;
}
//经管岗位 RowSpan
string curRowValue3 = (e.Row.DataItem as DataRowView)["AdminPost"].ToString().Trim();
string preRowValue3 = "";
preRowValue3 = (GridView4.Rows[e.Row.RowIndex - 1].FindControl("Label4") as Label).Text.Trim();
if (preRowValue3.Equals(curRowValue3))
{
GridViewRow gvr = e.Row;
gvr.Cells[5].Visible = false;
for (int ci = e.Row.RowIndex - 1; ci >= 0; ci--)
{
if (GridView4.Rows[ci].Cells[5].Visible)
{
if (GridView4.Rows[ci].Cells[5].RowSpan == 0)
GridView4.Rows[ci].Cells[5].RowSpan = 2;
else
GridView4.Rows[ci].Cells[5].RowSpan++;
break;
}
}
}
else
{
e.Row.Cells[5].Visible = true;
}
}
}
}
具体效果如图