Home c# How to fill the DataGridView columns using an array?

How to fill the DataGridView columns using an array?

Author

Date

Category

I often have to fill the DataGridView columns about the following contents:
B b b b b p p b b

I do this:

for (int i = 0; i & lt; 5; i ++)
 this.dgv [i, 0] .value = "b";
for (int i = 5; i & lt; 7; i ++)
 this.dgv [i, 0] .value = "P";
for (int i = 7; i & lt; 9; i ++)
 this.dgv [i, 0] .value = "b";

Maybe there is a way easier? For example, it is possible to somehow create an array (string [] record = {"b", "b", "b", "b", "b", "p", "p", "b", "B"}; ) and with it to fill the columns?


Answer 1, Authority 100%

Speech about Windows Forms?
If the number of columns is known and constantly, but create them in the DataGridView editor, then combine data into one array and insert this.datagridview.rows.add (Array) .

this.datagridview1.rows.clear ();
  Foreach (Var Array In Arrays)
  {
    this.datagridview1.rows.add (array);
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions