我在数据表中使用以下格式的数据,我试图绑定到gridview (格式-1)
Id col1 col2
20 a ac-1
20 a ac-2
20 a ac-3
20 a ac-4
21 a ac-1
21 a ac-2
21 a ac-3
21 a ac-4
在数据库之后我得到格式以下格式(格式-2)
Id col1 col2
20 a ac-1
ac-2
ac-3
ac-4
21 ac-1
ac-2
ac-3
ac-4
但我正在寻找以下格式在gridview中表示的格式(格式-3)
Id col1 col2
20 a ac-1
ac-2
ac-3
ac-4
21 a ac-1
ac-2
ac-3
ac-4
以下代码适用于grdiview中的Ondatabound事件
for (int i = gvConversionGrid.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = gvConversionGrid.Rows[i];
GridViewRow previousRow = gvConversionGrid.Rows[i - 1];
for (int j = 0; j < row.Cells.Count; j++)
{
if (row.Cells[j].Text == previousRow.Cells[j].Text)
{
if (previousRow.Cells[j].RowSpan == 0)
{
if (row.Cells[j].RowSpan == 0)
{
previousRow.Cells[j].RowSpan += 2;
}
else
{
previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
}
row.Cells[j].Visible = false;
}
}
}
}
有没有办法操纵gridview并为gridview数据提供格式,如 format-3 ..
请任何人帮忙解决这个非常感谢我的问题。 非常感谢提前
更新:我正在寻找以下格式图片
答案 0 :(得分:1)
您可以插入2个隐藏的列
Id col1 col2 groupindex groupcount
20 a 1 1 4
20 a 2 2 4
20 a 3 3 4
20 a 4 4 4
21 a 1 1 5
21 a 2 2 5
21 a 3 3 5
21 a 4 4 5
21 a 5 5 5
并设置rowspan:
void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
// set rowspan for the first row of group
if (Convert.ToInt32(e.Row.Cells[3]) == 1)
{
var rowSpan = Convert.ToString(e.Row.Cells[4]);
e.Row.Cells[0].Attributes.Add("rowspan", rowSpan);
e.Row.Cells[1].Attributes.Add("rowspan", rowSpan);
}
else
{
e.Row.Cells[0].Visible = false;
e.Row.Cells[1].Visible = false;
}
}
}
答案 1 :(得分:1)
您可以将之前的Id
值设置为在方法外声明的可变量,并将当前行与GridView的OnRowDataBound
事件中的行进行比较。
string previousCellValue = "";
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a row
DataRowView row = e.Row.DataItem as DataRowView;
//check if the current id matches the previous row
if (previousCellValue == row["Id"].ToString())
{
//clear the first cell
e.Row.Cells[0].Text = "";
//apply column span
e.Row.Cells[0].ColumnSpan = 2;
e.Row.Cells[1].Visible = false;
}
else
{
previousCellValue = row["Id"].ToString();
}
}
}
结果:
更新
string previousCellValue = "";
int previousCellCount = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow and not the first row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a row
DataRowView row = e.Row.DataItem as DataRowView;
//check if the current id matches the previous row
if (previousCellValue == row["Id"].ToString())
{
//count the number of same cells
previousCellCount++;
}
else
{
//span the rows for the first two cells
if (previousCellCount > 1)
{
GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[0].RowSpan = previousCellCount;
GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[1].RowSpan = previousCellCount;
//hide the other cells in the column
for (int i = 1; i < previousCellCount; i++)
{
GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[0].Visible = false;
GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[1].Visible = false;
}
}
previousCellValue = row["Id"].ToString();
previousCellCount = 1;
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
//use the footer row to create spanning for the last rows if needed
if (previousCellCount > 1)
{
GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[0].RowSpan = previousCellCount;
GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[1].RowSpan = previousCellCount;
//hide the other cells in the column
for (int i = 1; i < previousCellCount; i++)
{
GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[0].Visible = false;
GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[1].Visible = false;
}
}
}
}
结果: