ControlExtensions.AddDataGridView Method (ControlCollection, Range, String)
Adds a new DataGridView control to the worksheet at the range specified.
Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)
Syntax
'Declaration
<ExtensionAttribute> _
Public Shared Function AddDataGridView ( _
controls As ControlCollection, _
range As Range, _
name As String _
) As DataGridView
public static DataGridView AddDataGridView(
this ControlCollection controls,
Range range,
string name
)
Parameters
controls
Type: Microsoft.Office.Tools.Excel.ControlCollectionThe collection to add the control to. Do not supply this parameter yourself. When you call this method on the collection returned by the Worksheet.Controls property (in an application-level project) or the WorksheetBase.Controls property (in a document-level project), this parameter is supplied automatically.
range
Type: Microsoft.Office.Interop.Excel.RangeA Range that provides the bounds for the control.
name
Type: System.StringThe name of the control that can be used to index the control in the ControlCollection instance.
Return Value
Type: Microsoft.Office.Tools.Excel.Controls.DataGridView
The DataGridView control that was added to the ControlCollection instance.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type ControlCollection. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | The name or range argument is nulla null reference (Nothing in Visual Basic), or the name argument has zero length. |
ControlNameAlreadyExistsException | A control with the same name is already in the ControlCollection instance. |
InvalidRangeException | The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as the ControlCollection instance. |
Remarks
The AddDataGridView method enables you to add DataGridView objects to the end of the ControlCollection. To remove a DataGridView that was previously added programmatically, use the Remove method.
The control automatically resizes when the range is resized.
Examples
The following code example adds a DataGridView control to cells A1 through D9, and then adds two columns to the control. To use this example, run it from the Sheet1 class in a document-level project.
Private Sub ExcelRangeAddDataGridView()
Dim DataGridView1 As Microsoft.Office.Tools.Excel. _
Controls.DataGridView = Me.Controls. _
AddDataGridView(Me.Range("A1", "D9"), _
"DataGridView1")
DataGridView1.Columns.Add("firsName", "First Name")
DataGridView1.Columns.Add("lastName", "Last Name")
End Sub
private void ExcelRangeAddDataGridView()
{
Microsoft.Office.Tools.Excel.Controls.DataGridView
dataGridView1 = this.Controls.AddDataGridView(
this.Range["A1", "D9"], "dataGridView1");
dataGridView1.Columns.Add("firsName", "First Name");
dataGridView1.Columns.Add("lastName", "Last Name");
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.