我的问题与之前有关stackoverflow的问题有关。在我之前的问题中,我需要在ac#asp.net Gridview Control中合并具有相同值的列。其他stackoverflowers的出色帮助和支持解决了此问题,但是我还需要按行级别对顺序进行排序,例如:
gridview控件允许我按列(从上到下)对值进行排序,但是如果您查看(当前格式)屏幕截图并检查行,则可以使用一种方法对行进行排序,例如:在行级别上从左到右3,您将看到以下结果:(2a,2c,2x2a,2c)现在,理想情况下,我想将它们分组为(3x2a,2x2c),但是不确定这是否是正确的方法,因为它将涉及到移位我可以想象,如果我们正确地对第一行进行排序并转到第二行,我们可能会再次弄乱第一行的结果,不确定这是否可行甚至可能。
我想让我的gridview控件按如下所示显示数据:
如果您需要任何其他信息,请询问,我将尝试扩大我的问题。
更新信息:
在ConnersFan的建议之后,我得到了以下结果,这个想法有点儿存在,但还不完全是。
运行页面后,我得到此屏幕截图,在每个屏幕截图之后,我将解释发生的情况:
^^^ DEFAULT PAGE LOAD:这会将行单元格中的相等值合并/分组在一起。
^^^单击第一行“排序”链接后,第一行看起来正确,列标题和值都移到了正确的位置。
^^^单击第二行的“排序”链接后,它将正确地对第二行进行排序,并且没有弄乱第一行
^^^ After clicking on the third row 'sort' link, it messes up the first and second row, but managed to merge and group the third row ok.
^^^ After clicking on the fourth row 'sort' link, it messes up the first, second and third row, but managed to merge and group the fourth row ok.
^^^ After clicking on the fifth row 'sort' link, (which should not make a difference as they are all the same equal value, it messes up the first, second, third and fourth row, but managed to merge and group the fifth row still ok.
^^^ After clicking on the sixth row 'sort' link, (which should not make a difference as they are all different values, it messes the rest up.
So, what i would love to see is that, we dont need to sort these with a button but just on page load all rows are correctly merged and grouped, without messing up the previous row and we should end up, if done correctly, with the required format screenshot, so the order of the columns is determined by the values in the row cells, if they are equal.
Thank you guys ever so much, you have helped me out greatly already.
If I understand your question correctly, you want to transpose the usual sort command, so that it rearranges the GridView columns according to the order in a row rather than rearranging the rows according to the order in a column.
You can insert a LinkButton in the first column to trigger the sort on a specific row (I used a TemplateField instead of a ButtonField in order to set the text with a binding expression):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
OnRowCommand="GridView1_RowCommand" OnPreRender="GridView1_PreRender">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# "Row " + ((Container as GridViewRow).RowIndex + 1) %>' CommandName="SortRow" CommandArgument='<%# (Container as GridViewRow).RowIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在后面的代码中,您可以在网格的左侧定义固定列的数量,并sortRowIndex
在RowCommand
事件处理程序中设置一个变量:
private const int fixedColumnCount = 1; // Row number column
private int sortRowIndex = -1;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SortRow")
{
sortRowIndex = Convert.ToInt32(e.CommandArgument);
// Here set the data source and bind the data to the GridView
GridView1.DataSource = ...
GridView1.DataBind();
}
}
排序本身可以在数据源中执行,正如fnostro在您之前的文章中所建议的那样。由于我不知道数据是什么样子,因此这是在网格中进行处理的一种方法。
这项工作可以在完成PreRender
或在DataBound
GridView控件的事件。您在另一篇文章中给出的将具有相同内容的单元格分组的代码也应在此事件处理程序中移动,并放在sort操作之后。
protected void GridView1_PreRender(object sender, EventArgs e)
{
List<int> sortedColIndexes = null;
if (sortRowIndex >= 0)
{
// Gather the cell data on the sorted row...
List<KeyValuePair<string, int>> cellsToSort = new List<KeyValuePair<string, int>>();
sortedColIndexes = new List<int>();
GridViewRow sortedRow = GridView1.Rows[sortRowIndex];
for (int i = fixedColumnCount; i < sortedRow.Cells.Count; i++)
{
TableCell cell = sortedRow.Cells[i];
cellsToSort.Add(new KeyValuePair<string, int>(cell.Text, i));
}
// ... and sort the cell indexes according to the cell content
sortedColIndexes = cellsToSort.OrderBy(x => x.Key).Select(x => x.Value).ToList();
}
RearrangeRowCells(GridView1.HeaderRow, sortedColIndexes);
foreach (GridViewRow row in GridView1.Rows)
{
RearrangeRowCells(row, sortedColIndexes);
// The code below is taken from the other post
// It merges the cells with the same content
for (int i = 0; i < row.Cells.Count - 1; i++)
{
TableCell cell = row.Cells[i];
if (cell.Visible)
{
int colSpanValue = 1;
for (int j = i + 1; j < row.Cells.Count; j++)
{
TableCell otherCell = row.Cells[j];
if (otherCell.Text == cell.Text)
{
colSpanValue++;
otherCell.Visible = false;
}
else
{
break;
}
}
if (colSpanValue > 1)
{
cell.ColumnSpan = colSpanValue;
cell.BackColor = System.Drawing.Color.Beige;
cell.HorizontalAlign = HorizontalAlign.Center;
}
}
}
}
}
// Utility function which performs the cell permutations on a given row
private void RearrangeRowCells(GridViewRow row, List<int> sortedColIndexes)
{
if (sortedColIndexes != null)
{
List<TableCell> sortedCells = new List<TableCell>();
foreach (int cellIndex in sortedColIndexes)
{
sortedCells.Add(row.Cells[cellIndex]);
}
for (int i = fixedColumnCount; i < sortedCells.Count - 1; i++)
{
row.Cells.Remove(sortedCells[i]);
row.Cells.AddAt(i + fixedColumnCount, sortedCells[i]);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句