0
我想僅爲行號擴展帶有(只讀)列的dategridview。 當datagridview按其他列內容排序時(例如excel),行號的排序不應改變! 是可能的?DataGridview中不可更改的行號列
我想僅爲行號擴展帶有(只讀)列的dategridview。 當datagridview按其他列內容排序時(例如excel),行號的排序不應改變! 是可能的?DataGridview中不可更改的行號列
我們可以通過以下兩種方式之一枚舉每一行:
private void AddIndexCol()
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = "Index";
col.HeaderText = "Index";
col.ReadOnly = true;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.CellTemplate = cell;
this.dataGridView1.Columns.Insert(0, col);
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Value = String.Format("{0}", e.RowIndex + 1);
e.FormattingApplied = true;
}
}
信用顯示中添加的列到ASh爲CellFormatting
代碼。
中的rowHeader
public Form1()
{
InitializeComponent();
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.RowHeadersDefaultCellStyle = style;
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRowHeaderCell header = this.dataGridView1.Rows[e.RowIndex].HeaderCell;
if (e.ColumnIndex == 0) // (header.Value == null)
{
header.Value = String.Format("{0}", e.RowIndex + 1);
}
}
說明顯示有關if
聲明。條件e.ColumnIndex == 0
將始終通過排序保留數字順序,而條件header.Value == null
將使用原始行保留行號(但在處理行刪除時將需要額外的代碼)。例如,這種降序排列:
Col == 0 Header == null
1 a => 1 c 1 a => 3 c
2 b 2 b 2 b 2 b
3 c 3 a 3 c 1 a
您可以顯示一些代碼來演示您到目前爲止嘗試過的嗎? – 2015-03-31 14:42:34