簡體   English   中英

如何對DataGridView中的重新排序列進行排序?

[英]How to chance column index on reordering column in DataGridView?

我正在將一些Excel工作表導入DataGridView。 我需要導入,在項目中對列進行重新排序,然后獲取新索引。

我已經嘗試過DataGridViewColumn.Index,DataGridViewColumn.DisplayIndex,Refresh(),但是索引永遠不會改變。

請問我該怎么做?

謝謝。

創建列時將創建DataGridView列索引。 如果要在GUI中更改順序,則要更改DisplayIndex,如果要更改DataGridView中的順序,則可以創建新的DataGridView或將列放在臨時列表中,然后刪除所有列並再次添加。

如果要更改數據源中元素的實際索引,則需要更新數據源結構。 但是如果需要更改列的顯示位置,則可以使用DisplayIndex屬性。 這是更改列索引的示例:

var items = new List<dynamic>
{
    new
    {
        Element1 = "Test Value",
        Id = 1
    },
    new
    {
        Element1 = "Test Value",
        Id = 1
    }
};
dataGridView1.DataSource = items;
dataGridView1.Columns[1].DisplayIndex = 0;
MessageBox.Show(dataGridView1[0, 0].Value.ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM