enter image description here There is a base in the access, and the c # application, on the form of 2 grids, on the left of the accessory base, on the right, added the grid itself, which is not tied to any table. How to make, that in the second table row and column headers are filled with selected rows in 1 table? I tried to do this:

//Определяем количество выбраных строк int n = t1DataGridView.SelectedRows.Count; //Создаем массив этого размера для хранения названий строк string[] labels = new string[n]; DataTable dt = new DataTable(); dataGridView1.DataSource = dt; for (int i = 0; i < n; i++) { labels[i]=Convert.ToString(t1DataGridView.SelectedRows[i].Cells[0].Value); } for (int i = 0; i < n; i++) { dt.Columns.Add(labels[i]); dt.Rows.Add(); dataGridView1.Rows[i].HeaderCell.Value = labels[i]; } 

But for some reason only the last line header is saved. How to make, that all headings in line are displayed? on the columns, everything seems to be normal.

    1 answer 1

    Try this:

      //Определяем количество выбраных строк int n = t1DataGridView.SelectedRows.Count; //Создаем массив этого размера для хранения названий строк string[] labels = new string[n]; DataTable dt = new DataTable(); for (int i = 0; i < n; i++) { labels[i]=Convert.ToString(t1DataGridView.SelectedRows[i].Cells[0].Value); dt.Columns.Add(labels[i]); dt.Rows.Add(); } dataGridView1.DataSource = dt; for (int i = 0; i < n; i++) { dataGridView1.Rows[i].HeaderCell.Value = labels[i]; } 
    • It worked, thanks. There are more questions: 1) when selecting, for example, in 1 table 3 rows in a row from top to bottom, in the second table row headers are displayed in reverse order, can this be corrected? 2) When you click on the column header to sort, all row header names are cleared, here you can either correct or turn off sorting. Questions not particularly critical for my program - bfdbndfnd
    • @bfdbndfnd If you have a new question, ask it using the " Ask a Question " button. If you need to specify the context, give a link to this question. - Nicolas Chabanovsky