N
N
Nikolai Novosad2015-11-23 15:51:51
C++ / C#
Nikolai Novosad, 2015-11-23 15:51:51

Ordinal number in GridView when displaying information from text?

Hello.
I display the data from the file in the dataGridView in the following way:

string[] arr = System.IO.File.ReadAllLines(filename, Encoding.Default);
 
            for (int i = 0; i < arr.Length; i++)
            {
                // вывод
                dataGridView1.Rows.Add(arr[i].Split(" ".ToCharArray()));
            }

There are 2 lines in the file and everything is displayed as it should. But it is necessary to make another column serial number. To display like this:
1 text1 text2
2 text3 text4
Tried several options, but it doesn't work.
How to change this code to display the serial number?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MonkAlex, 2015-11-23
@MonkAlex

Pushing data from a file directly into the grid is not the best way to work.
It is best to make an intermediate model, and at least the line class in which you will have an Id - just the line number, well, in addition to Id, you can make one - two string variables into which you will fill in data from the file.
Or, if there can be more than two of them, then I would declare line to be the heir of some kind of list and add words to it, another thing is that it is much more difficult to display such crap.
And the conclusion to the grid of this is simple. After reading the file, you will have some kind of List, and you simply write:
datagridview.ItemSources = myListOfLines;

M
Melz, 2015-11-23
@melz

There are two ways to feng shui. Yours, as already said, is wrong.
1. Use an event
2. Read in Data.Table, bind it to DataGridView (one line).
And he has a field called DataColumn.AutoIncrement, here . It will itself increase by the step that you set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question