Databinding, Dataset, Dataadapter and Datagridview: What are they for, how to use and in what order?

Asked

Viewed 1,327 times

9

I’ve been trying to learn how to use Datagridview control for weeks (yes, weeks).

Connecting to the bank using the designer is very easy, but I want to do it by code and I can’t.

I saw some tutorials on the web, but although following a step by step is easy, I could not understand how everything works, so I could do the process as I want.

From what I understand the property DataSource of DataGridView must receive a DataSet, which in turn seems to me to have some relationship with DataAdapters , but I can’t understand any of it. Microsoft’s documentation instead of helping left me even more confused.

I would like to know then the following things:

  1. What is the purpose of each of these items.
  2. How to use them to connect me to the bank without using the designer.
  3. In which sequence should I use them to retrieve bank information through control DataGridView.
  • I’m finding this question too broad.

  • I thought this might happen. What do you recommend I do, since there are several problems in one.

  • 1

    Separate as much as possible. It is better to ask 30 questions that answer a paragraph than a question that generates 30 paragraphs. There are many classes, asking various things, and asking what in the background is a complete tutorial. Divide and conquer is the secret. Go one step at a time. There are several advantages to this. In the end you get everything you need, help other people and everything organized. And earn + rep :)

1 answer

6


I’ll answer your questions in order.

What is the purpose of each of these items.

Data Binding

This is the only one that is not a component, but a concept. It enunciates how data components connect to each other.

What can cause some confusion is that the component DataGridView has a property called DataBindings (in the same plural), it is used to describe how to interpret a data source (see below).

DataSet

It’s a data set structured in application memory. It may have come from a database, a text file, an Excel spreadsheet, etc., what we call "data source" (Data Source, in English).

The class DataSet no . NET normally represents a set of one or more tables from a data source.

Here begins some confusion. This source of data that we talk about here is not exactly the same thing as the origin of visual component data, for example (such as property DataSource of DataGridView). She can be 4 things:

  1. An object that implements IList;
  2. An object that implements IListSource (here yes enter DataTable and DataSet);
  3. An object that implements IBindingList;
  4. An object that implements IBindingListView.

The most used are the first two. This amount of options can confuse a little, and I try to explain this further below.

DataGridView

The name already explains a little. It is a data grid view.

Being a view of a data grid, the component can only detail one list structure at a time. That is, if you connect a DataSet integer to component, you have to tell it which structure to use.

If the connected object implements IList, only the following construction is sufficient to link the data source to the DataGridView. Try to use too AutoGenerateColumns to create the columns according to the formatting of the list object:

List<MeuObjeto> meusObjetos = new List<MeuObjeto> { new MeuObjeto { Nome = "Cigano" } };
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = meusObjetos;

If the object is of type DataSet, it will be necessary to inform the properties DataMember and AutoGenerateColumns to the DataGridView:

DataGridView.AutoGenerateColumns = true;
DataGridView.DataSource = ds; // dataset obtido antes.
DataGridView.DataMember = "MeusObjetos";

And DataBindings?

Well, this is the tricky way to set up the DataGridView, not least because DataBindings applies to any object Control, then we would be talking, actually, of connecting column to column with each data source element. Examples of the use of DataBindings most common are with simpler components such as the TextBox and the DateTimePicker, and the examples with DataGridView are so difficult to find that it is not worth putting examples of use in the answer, even because the above methods well contemplate the link between grid component and data source.

How to use them to connect me to the bank without using the designer.

Programmatically speaking, it can be as below. Note that here I use a DataAdapter for the first time, whose function is to fill the object in memory with the database result:

string connString = "Data Source=SeuBanco; Initial Catalog=.\SQLEXPRESS; User Id=usuario; Password=senha";
string sql = "SELECT * FROM Tabela";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

con.Open();
cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable registros = new DataTable();
da.Fill(registros);

dataGridView.DataSource = registros;

In which sequence should I use them to retrieve information from the bank through the Datagridview control.

The order is usually the same:

  1. Bring the data into the data source, be it a List, DataTable or DataSet;
  2. Connect to DataGridView using the property DataSource together with the above guidelines.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.