C# gridview行和列合并
我在datatable中有以下格式的数据,我正试图将其绑定到gridview(格式-1) 在有了数据绑定后,我得到了以下格式的gridview(格式-2) 但我正在寻找以下数据格式,以便在gridview(格式3) 下面的代码用于grdiview中的Ondatabound事件C# gridview行和列合并,c#,asp.net,c#-4.0,datatable,c#-3.0,C#,Asp.net,C# 4.0,Datatable,C# 3.0,我在datatable中有以下格式的数据,我正试图将其绑定到gridview(格式-1) 在有了数据绑定后,我得到了以下格式的gridview(格式-2) 但我正在寻找以下数据格式,以便在gridview(格式3) 下面的代码用于grdiview中的Ondatabound事件 for (int i = gvConversionGrid.Rows.Count - 1; i > 0; i--) { GridViewRow row = gvConversionGrid
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;
}
}
}
}
for(int i=gvConversionGrid.Rows.Count-1;i>0;i--)
{
GridViewRow行=gvConversionGrid.Rows[i];
GridViewRow-previousRow=gvConversionGrid.Rows[i-1];
对于(int j=0;j
是否有任何方法可以操作gridview并将gridview数据的格式设置为格式-3
请任何人帮我解答这个问题,我会非常感激的。。
非常感谢
更新:我正在寻找以下格式的图像
您可以插入两个隐藏列
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
并设置行跨度:
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;
}
}
}
您可以将上一个
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;
}
}
}
}
字符串previousCellValue=“”;
int-previousCellCount=1;
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//检查该行是否为datarow,而不是第一行
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//将数据项强制转换回行
DataRowView行=e.row.DataItem作为DataRowView;
//检查当前id是否与上一行匹配
if(previousCellValue==行[“Id”].ToString())
{
//计算相同单元格的数量
previousCellCount++;
}
其他的
{
//跨越前两个单元格的行
如果(上一个单元格计数>1)
{
GridView1.Rows[e.Row.RowIndex-previousCellCount]。单元格[0]。RowSpan=previousCellCount;
GridView1.Rows[e.Row.RowIndex-previousCellCount]。单元格[1]。RowSpan=previousCellCount;
//隐藏列中的其他单元格
对于(int i=1;i1)
{
GridView1.Rows[GridView1.Rows.Count-previousCellCount]。单元格[0]。RowSpan=previousCellCount;
GridView1.Rows[GridView1.Rows.Count-previousCellCount]。单元格[1]。RowSpan=previousCellCount;
//隐藏列中的其他单元格
对于(int i=1;i
结果:
是否完全不可能,是否有人对此合并行有任何建议..格式1是否来自一个表?它应该只绑定到一个GridView吗???@Asif.Ali它只是一个表单数据表。。。。是的,它只绑定到一个gridview…所有行的col1值都相同,只有Id和col2不同?@Asif.Ali抱歉,我无法为您获取所有行的col1值都相同的意思?我在onDataBound中进行了该操作。。您在rowDataBound事件中正确地指定了方法。是的,它是OnRowDataBound事件。但我在第二列中只得到一个“a”。。我不明白你最后的评论,你能建议做些什么改变吗。我的代码片段将产生您所指出的确切结果。请看添加的屏幕截图。我在问题中附上了图片,您能建议对该图片进行任何更改以获得网格形式的图片吗..谢谢您的建议。。我需要在哪里插入组索引和组计数,以及如何将其填充到gridview。。。
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;
}
}
}
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;
}
}
}
}