以前都是这么写的void dataGridView列表_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
e.Value=e.RowIndex+1;
}这种写法在遇到 DataGridViewRow.Visible=false时会有问题,RowIndex代表的是行在集合中的的索引号,这个集合包含了非可见行。大家有什么办法?

解决方案 »

  1.   

    如果想把隐藏的行过滤不计数的话,那么需要动态进行计算了。
    int rowId=0;
    for(int i=0;i<=e.RowIndex;i++)
    {
      DataGridViewRow row = this.Grid1.Rows[i];
      if(row.Visible)
        rowId++;
    }
    然后在e.Value=rowId;
      

  2.   

    数据量大的话循环会很慢,我觉得这应该是个比较基本的功能,得到某行在列表中的序号。.Net有下面的方法
    dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible);//得到可见行的总数
    dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible);//得到可见行的累加高度难道就没有现成的 dataGridView.Rows.GetRowIndex(int rowIndex,DataGridViewElementStates states)???
      

  3.   

    隐藏行是对用户来说的,但是grid中还是真是存在的。
    本来行就有RowIndex的,而你的行索引太怪异,微软也不是万能的。
      

  4.   

            private void dgvGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                                                    e.RowBounds.Location.Y,
                                                    this.dataGridView1.RowHeadersWidth - 4,
                                                    e.RowBounds.Height);            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                        this.dataGridView1.RowHeadersDefaultCellStyle.Font,
                                        rectangle,
                                        this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                                        TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
      

  5.   

    在dgvGridView的rowPostPost选择这个事件就行了。
      

  6.   

    <%#Container.ItemIndex+1 %>
      

  7.   

    我觉得我的需求一点也不怪异,用户需要过滤一些行,我做了类似Excel的功能,在列上点右键,弹出的下拉列表中显示了该列数据的“GROUP”
    用户可以选择和切换需要查看的内容
      

  8.   

    qdlaole:绕了一大圈最后还是使用的 RowIndex