DataGridViewColumnCollection.GetColumnCount(DataGridViewElementStates) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回符合指定之篩選條件需求的資料行數。
public:
int GetColumnCount(System::Windows::Forms::DataGridViewElementStates includeFilter);
public int GetColumnCount (System.Windows.Forms.DataGridViewElementStates includeFilter);
member this.GetColumnCount : System.Windows.Forms.DataGridViewElementStates -> int
Public Function GetColumnCount (includeFilter As DataGridViewElementStates) As Integer
參數
- includeFilter
- DataGridViewElementStates
表示包含篩選條件之 DataGridViewElementStates 值的位元 (Bitwise) 組合。
傳回
符合篩選條件需求的資料行數。
例外狀況
includeFilter
不是 DataGridViewElementStates 值的有效位元組合。
範例
下列程式碼範例說明如何使用這個方法來取得選取的資料行數目。
private void selectedColumnsButton_Click(object sender, System.EventArgs e)
{
Int32 selectedColumnCount = dataGridView1.Columns
.GetColumnCount(DataGridViewElementStates.Selected);
if (selectedColumnCount > 0)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < selectedColumnCount; i++)
{
sb.Append("Column: ");
sb.Append(dataGridView1.SelectedColumns[i].Index
.ToString());
sb.Append(Environment.NewLine);
}
sb.Append("Total: " + selectedColumnCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Columns");
}
}
Private Sub selectedColumnsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedColumnsButton.Click
Dim selectedColumnCount As Integer = dataGridView1.Columns _
.GetColumnCount(DataGridViewElementStates.Selected)
If selectedColumnCount > 0 Then
Dim sb As New System.Text.StringBuilder()
Dim i As Integer
For i = 0 To selectedColumnCount - 1
sb.Append("Column: ")
sb.Append(dataGridView1.SelectedColumns(i).Index.ToString())
sb.Append(Environment.NewLine)
Next i
sb.Append("Total: " + selectedColumnCount.ToString())
MessageBox.Show(sb.ToString(), "Selected Columns")
End If
End Sub