It is really not as simple as your questions appears.
A
DataGridView
consists of
Rows
and
Columns
, each of which can be addressed separately. It does not matter where the data comes from, what is important is to understand how to add data to the
DataGridView
either per
Row
or
Cell
.
To add a new row to a
DataGridView
you simply use the
.Rows.Add
syntax. However it is important to understand that when adding a row to a
DataGridView
, you have to separate the data according to the
Columns
you have have in the
DataGridView
.
For example: You have a
DataGridView
with 3 columns, each of them holding different data e.g.: First Name, Middle Name, Last Name. Now you wish to add the following information: John S Doe. Your syntax will look something like this:
DataGridView.Rows.Add("John", "S", "Doe")
If the information comes from TextBoxes e.g.:
txtFirstName
txtMiddleName
txtLastName
Your code will look something like this:
DataGridView.Rows.Add(txtFirstName.Text, txtMiddleName.Text, txtLastName.Text)
As you can see, it does not matter where the data comes from, it matters how you insert that into the
DataGridView
.
As for inserting data in specified locations (
Cells
) in the
DataGridView
, you can use the following code as a stepping stone to get you going:
DataGridView.Rows(DataGridView.Rows.Count - 1).Cells(1).Style.BackColor = Color.Red
What the above code does is to change the
BackGroundColor
of a specific
Cell
in the
DataGridView
. This is merely a snippet from actual code and should be treated as such.
Hope this helps.
In future try to show us what you have tried because we would hate to do your homework for you as you will not learn anything if we keep doing your homework for you.