Aleksa
Aleksa

Reputation: 3104

Binding Datagridview to dataset

I have a problem binding datagridview to dataset, when I debug code I can see that dataset is filled ok with data from database, but it wont show on datagridview... Here is my code:

        MySqlConnection conn = new MySqlConnection(connectionString);
        string sql = @"select artikli.idArtikla, artikli.NazivArtikla
                      from artikli";
        MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Artikli");

        // Bind the data table to the data grid
        dataGridView1.DataSource = ds;

EDIT: Thx for answers, now when I can see my data in grid, what is the easiest way to allow inserting, deleting and editing in my grid and forwarding those cnages to my database table?

Upvotes: 1

Views: 2684

Answers (3)

Sanu Uthaiah Bollera
Sanu Uthaiah Bollera

Reputation: 937

You forgot to bind the dataset to gridview.

At the end of your code add the following line.. after datasource.

dataGridView1.DataBind();

Upvotes: -1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26199

You need to provide the which DataTable you want to bind to DataGridView.

Try:

dataGridView1.DataSource = ds.Tables[0];

Upvotes: 2

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

DataGridView doesn't know how to bind DataSet, you must bind a DataTable.

dataGridView1.DataSource = ds.Tables["Artikli"];

or

dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Artikli";

Upvotes: 3

Related Questions