-2

I am new to C# programming and topic of operating with jagged array.

I have some data stored in my string[][] arrayname and want to show it in datagridview.

Will be very grateful if you could advice me on the case.

YutuKAron
  • 3
  • 1
  • 1
    You are also new to StackOverflow which is not like typical internet forums. I advise you to read [ask] and take the [tour]. And maybe get your Google fixed - 250 posts here already on that topic. – Ňɏssa Pøngjǣrdenlarp Feb 05 '18 at 01:45

1 Answers1

0

You need to create dataset, usually I use a DataTable, I have drafted a solution to your problem, but you have to using Linq:

var ListName = arrayname.ToList();
//get number of column, probalby you dont need it
int cols = ListName.Select(r => r.Length).Max();

//Create a datasource
DataTable dt = new DataTable();

//Write column, probalby you dont need it
for (int f = 0; f < cols; f++)
    dt.Columns.Add("Col " + (f+1));

foreach (var row in ListName) {
    //make a row
    List<string> Lrow = new List<string>();
    Lrow.AddRange(row);
    //if row is too short add fields
    if (Lrow.Count < cols)
        for (int i = Lrow.Count; i < dt.Columns.Count; i++) {
            Lrow.Add("");
        }
    //at last add row to dataTable
    dt.Rows.Add(Lrow.ToArray());
}
//and set dataGridView's DataSource to DataTable
dataGridView1.DataSource = dt;

The result should be this

br1 Brown
  • 16
  • 1
  • Is it possible to make it so that column headers in dataGridView are the first row array attributes, know it is a bit messy, as the number of attributes on the first row may be less than in coming next rows. But lets assume that the number of attributes on each are the same. So column headers will be a[0][], and body a[1][]....? Thank you very much :) – YutuKAron Feb 05 '18 at 13:01
  • @YutuKAron I created a number of columns equal to the length of the longest array `cols` is the maxlength i `Select(r => r.Length)` select all Lengths and i get the max `.Max()` – br1 Brown Feb 05 '18 at 13:14