Export (0) Print
Expand All
Expand Minimize
5 out of 22 rated this helpful - Rate this topic

DataGridView.DataSource Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the data source that the DataGridView is displaying data for.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public Object DataSource { get; set; }
/** @property */
public Object get_DataSource ()

/** @property */
public void set_DataSource (Object value)

public function get DataSource () : Object

public function set DataSource (value : Object)

Property Value

The object that contains data for the DataGridView to display.
Exception typeCondition

Exception

An error occurred in the data source and either there is no handler for the DataError event or the handler has set the DataGridViewDataErrorEventArgs.ThrowException property to true. The exception object can typically be cast to type FormatException.

The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

Typically, you will bind to a BindingSource component and bind the BindingSource component to another data source or populate it with business objects. The BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically.

When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to. When binding to a BindingSource component that contains multiple lists or tables, however, you can set the DataMember property of the BindingSource component instead.

When binding to an object collection rather than to database data, you will typically set the DataSourceNullValue property of the object returned by the DefaultCellStyle property to a null reference (Nothing in Visual Basic) rather than using the default value of System.DbNull.Value, which is appropriate for database data.

For more information, see DataGridView Control Overview (Windows Forms).

The following code example demonstrates how to initialize a simple data-bound DataGridView. It also demonstrates how to set the DataSource property. To run this example, paste the following code into a form that contains a DataGridView named dataGridView1, replace the value of the connectionString variable specified in the code with a string that is valid for the system the example will run on, and call the InitializeDataGridView method from the form's constructor or Load event handler.

private void InitializeDataGridView()
{
    try
    {
        // Set up the DataGridView.
        dataGridView1.Dock = DockStyle.Fill;

        // Automatically generate the DataGridView columns.
        dataGridView1.AutoGenerateColumns = true;

        // Set up the data source.
        bindingSource1.DataSource = GetData("Select * From Products");
        dataGridView1.DataSource = bindingSource1;

        // Automatically resize the visible rows.
        dataGridView1.AutoSizeRowsMode =
            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

        // Set the DataGridView control's border.
        dataGridView1.BorderStyle = BorderStyle.Fixed3D;

        // Put the cells in edit mode when user enters them.
        dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
    }
    catch (SqlException)
    {
        MessageBox.Show("To run this sample replace connection.ConnectionString" +
            " with a valid connection string to a Northwind" +
            " database accessible to your system.", "ERROR",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        System.Threading.Thread.CurrentThread.Abort();
    }
}

private static DataTable GetData(string sqlCommand)
{
    string connectionString = "Integrated Security=SSPI;" +
        "Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost";

    SqlConnection northwindConnection = new SqlConnection(connectionString);

    SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);

    return table;
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.