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).
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:
- An object that implements
IList
;
- An object that implements
IListSource
(here yes enter DataTable
and DataSet
);
- An object that implements
IBindingList
;
- 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.
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";
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:
- Bring the data into the data source, be it a
List
, DataTable
or DataSet
;
- Connect to
DataGridView
using the property DataSource
together with the above guidelines.
I’m finding this question too broad.
– Maniero
I thought this might happen. What do you recommend I do, since there are several problems in one.
– Ezequiel Barbosa
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 :)
– Maniero