DataGridViewColumnCollection.Insert(Int32, DataGridViewColumn) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在集合中的给定索引处插入列。
public:
virtual void Insert(int columnIndex, System::Windows::Forms::DataGridViewColumn ^ dataGridViewColumn);
public virtual void Insert(int columnIndex, System.Windows.Forms.DataGridViewColumn dataGridViewColumn);
abstract member Insert : int * System.Windows.Forms.DataGridViewColumn -> unit
override this.Insert : int * System.Windows.Forms.DataGridViewColumn -> unit
Public Overridable Sub Insert (columnIndex As Integer, dataGridViewColumn As DataGridViewColumn)
参数
- columnIndex
- Int32
要插入给定列的从零开始的索引。
- dataGridViewColumn
- DataGridViewColumn
要 DataGridViewColumn 插入的。
例外
dataGridViewColumn 是 null。
关联的 DataGridView 控件正在执行下列操作之一,暂时阻止添加新列:
选择控件中的所有单元格。
清除所选内容。
更新列 DisplayIndex 属性值。
-或-
此方法是从以下事件之一 DataGridView 的处理程序调用的:
-或-
dataGridViewColumn 已属于控件 DataGridView 。
-或-
属性值dataGridViewColumnSortMode为和AutomaticSelectionMode属性值为FullColumnSelect或 ColumnHeaderSelect。 使用控件 ISupportInitialize.BeginInit() 和 ISupportInitialize.EndInit() 方法暂时设置冲突的属性值。
-或-
属性值 dataGridViewColumnInheritedAutoSizeMode 为 ColumnHeaderColumnHeadersVisible ,属性值为 false.
-或-
dataGridViewColumn
InheritedAutoSizeMode具有属性值的Fill属性值和Frozen属性值 。true
-或-
dataGridViewColumn has DisplayIndex 和 Frozen 属性值,用于在具有相反 Frozen 属性值的一组相邻列之间显示该值。
-或-
该DataGridView控件至少包含一行,其dataGridViewColumnCellType属性值为 null.
示例
下面的代码示例演示了此方法的使用。 有关详细信息,请参阅 How to: Add an Unbound Column to a Data-Bound Windows 窗体 DataGridView Control。
private void CreateUnboundButtonColumn()
{
// Initialize the button column.
DataGridViewButtonColumn buttonColumn =
new DataGridViewButtonColumn();
buttonColumn.Name = "Details";
buttonColumn.HeaderText = "Details";
buttonColumn.Text = "View Details";
// Use the Text property for the button text for all cells rather
// than using each cell's value as the text for its own button.
buttonColumn.UseColumnTextForButtonValue = true;
// Add the button column to the control.
dataGridView1.Columns.Insert(0, buttonColumn);
}
Private Sub CreateUnboundButtonColumn()
' Initialize the button column.
Dim buttonColumn As New DataGridViewButtonColumn
With buttonColumn
.HeaderText = "Details"
.Name = "Details"
.Text = "View Details"
' Use the Text property for the button text for all cells rather
' than using each cell's value as the text for its own button.
.UseColumnTextForButtonValue = True
End With
' Add the button column to the control.
dataGridView1.Columns.Insert(0, buttonColumn)
End Sub