以前都是这么写的void dataGridView列表_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
e.Value=e.RowIndex+1;
}这种写法在遇到 DataGridViewRow.Visible=false时会有问题,RowIndex代表的是行在集合中的的索引号,这个集合包含了非可见行。大家有什么办法?
{
e.Value=e.RowIndex+1;
}这种写法在遇到 DataGridViewRow.Visible=false时会有问题,RowIndex代表的是行在集合中的的索引号,这个集合包含了非可见行。大家有什么办法?
int rowId=0;
for(int i=0;i<=e.RowIndex;i++)
{
DataGridViewRow row = this.Grid1.Rows[i];
if(row.Visible)
rowId++;
}
然后在e.Value=rowId;
dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible);//得到可见行的总数
dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible);//得到可见行的累加高度难道就没有现成的 dataGridView.Rows.GetRowIndex(int rowIndex,DataGridViewElementStates states)???
本来行就有RowIndex的,而你的行索引太怪异,微软也不是万能的。
{
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);
}
用户可以选择和切换需要查看的内容