Back to Devexpress

GridControl.DataSource Property

windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-a111cae8.md

latest7.8 KB
Original Source

GridControl.DataSource Property

Gets or sets the grid control’s data source.

Namespace : DevExpress.XtraGrid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue(null)]
[DXCategory("Data")]
public virtual object DataSource { get; set; }
vb
<DefaultValue(Nothing)>
<DXCategory("Data")>
Public Overridable Property DataSource As Object

Property Value

TypeDefaultDescription
Objectnull

An object representing the grid control’s data source.

|

Remarks

Refer to the Data Binding and Master-Detail Relationships topics for information on which objects can be assigned to the DataSource property.

After the DataSource property is set to null , the ColumnView.FocusedRowHandle of the GridControl.MainView is set to GridControl.InvalidRowHandle. After a new data source is assigned to the DataSource property, the ColumnView.FocusedRowHandle is set to 0, to refer to the first data record.

The code sample below illustrates how to bind the GridControl to XML data.

csharp
xmlDataSet.ReadXml(@".\products.xml");
gridControl1.DataSource = xmlDataSet.Tables["product"];
vb
xmlDataSet.ReadXml(".\products.xml")
gridControl1.DataSource = xmlDataSet.Tables("product")

The following code snippets (auto-collected from DevExpress Examples) contain references to the DataSource property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

report-server-how-to-manage-scheduled-jobs-from-a-windows-forms-application/CS/ScheduledTasksAPIClientDemo/MainForm.cs#L42

csharp
void FillScheduledJobListBox(IEnumerable<ScheduledJobCatalogItemDto> scheduledJobs) {
    scheduledJobsGrid.DataSource = scheduledJobs;
    scheduledJobsView.BestFitColumns();

winforms-mvvm-expenses-app/CS/MVVMExpenses/Views/Account/AccountsEditFormView.cs#L29

csharp
fluent.SetBinding(
    gridControl1, gc => gc.DataSource, x => x.AccountTransactionDetails.Entities);

XDL_how-to-convert-the-criteriaoperator-to-a-lambda-expression-to-use-it-for-iqueryable-e2596/CS/WindowsFormsApplication152/Form1.cs#L37

csharp
gridControl1.DataSource = source.ToList();
CreateFilterColumns();

winforms-gridlookupedit-multiple-item-selection/CS/Form1.cs#L109

csharp
private void InitGrid() {
    popupGridControl1.DataSource = CreateList(5);
    gridControl2.DataSource = CreateList(5);

winforms-grid-data-annotation-attributes/CS/GridDataAttributes/Form1.cs#L36

csharp
case "Company public info":
    gridControl1.DataSource = GridSampleDataList.GetCompanyPublicInfo();
    break;

report-server-how-to-manage-scheduled-jobs-from-a-windows-forms-application/VB/ScheduledTasksAPIClientDemo/MainForm.vb#L42

vb
Private Sub FillScheduledJobListBox(ByVal scheduledJobs As IEnumerable(Of ScheduledJobCatalogItemDto))
    scheduledJobsGrid.DataSource = scheduledJobs
    scheduledJobsView.BestFitColumns()

winforms-mvvm-expenses-app/VB/MVVMExpenses/Views/Account/AccountsEditFormView.vb#L29

vb
fluent.SetObjectDataSourceBinding(accountBindingSource, Function(x) x.Entity, Sub(x) x.Update())
fluent.SetBinding(GridControl1, Function(gc) gc.DataSource, Function(x) x.AccountTransactionDetails.Entities)
CType(GridControl1.MainView, DevExpress.XtraGrid.Views.Grid.GridView).Columns("Account").Visible = False

XDL_how-to-convert-the-criteriaoperator-to-a-lambda-expression-to-use-it-for-iqueryable-e2596/VB/WindowsFormsApplication152/Form1.vb#L29

vb
source = From product In products Where product.Quantity >= 20 Select product
gridControl1.DataSource = source.ToList()
CreateFilterColumns()

winforms-gridlookupedit-multiple-item-selection/VB/Form1.vb#L106

vb
Private Sub InitGrid()
    popupGridControl1.DataSource = CreateList(5)
    gridControl2.DataSource = CreateList(5)

winforms-grid-data-annotation-attributes/VB/GridDataAttributes/Form1.vb#L33

vb
Case "Company public info"
    gridControl1.DataSource = GridSampleDataList.GetCompanyPublicInfo()
Case "Company private info"

See Also

DataSource

AutoPopulateColumns

PopulateColumns

Data Binding

Master-Detail Relationships

GridControl Class

GridControl Members

DevExpress.XtraGrid Namespace