Convert DataGridView rows as columns in Windows Application

Last Reply one year ago By pandeyism

Posted one year ago

how to display datagridview rows as column and columns as rows in another datagridview using datatable 

I am using this code but its not working

        Dim dts As New DataTable
        dts.Columns.Add("SId")
        dts.Columns.Add("Shift")
        dts.Columns.Add("DDate")
        For i = 0 To DataGridView1.Rows.Count - 4
            For x = 0 To DataGridView1.Columns.Count - 4
                Dim dr As DataRow = dts.NewRow
                dr("SId") = DataGridView1.Item(0, i).Value.ToString
                dr("DDate") = DataGridView1.Item(2, x).Value.ToString
                'dr("Shift") = DataGridView1.CurrentCell.Value.ToString()
                'dr("DDate") = DataGridView1.Columns(msg).HeaderText.ToString.ToString()
                dts.Rows.Add(dr)
                dts.AcceptChanges()
            Next
        Next
        Me.DataGridView2.DataSource = dts

here is what I want

 

        Dim dt1 As DataTable = New DataTable()
        dt1.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("01-Jan-18"), New DataColumn("02-Jan-18"), New DataColumn("03-Jan-18"), New DataColumn("04-Jan-18")})
        dt1.Rows.Add("Ahemd", "1", "1", "OFF", "OFF")
        dt1.Rows.Add("Ajith", "3", "3", "2", "2")
        dt1.Rows.Add("Soumya", "OFF", "1", "1", "1")
        Me.DataGridView1.DataSource = dt1
 
        Dim dt2 As DataTable = New DataTable()
        dt2.Columns.Add("Name")
        dt2.Columns.Add("Duty")
        dt2.Columns.Add("Date")
        dt2.Rows.Add("Ahemd", "1", "01-Jan-2018")
        dt2.Rows.Add("Ahemd", "1", "02-Jan-2018")
        dt2.Rows.Add("Ahemd", "OFF", "03-Jan-2018")
        dt2.Rows.Add("Ahemd", "OFF", "04-Jan-2018")
        dt2.Rows.Add("Ajith", "3", "01-Jan-2018")
        dt2.Rows.Add("Ajith", "3", "02-Jan-2018")
        dt2.Rows.Add("Ajith", "2", "03-Jan-2018")
        dt2.Rows.Add("Ajith", "2", "04-Jan-2018")
        dt2.Rows.Add("Soumya", "OFF", "01-Jan-2018")
        dt2.Rows.Add("Soumya", "1", "02-Jan-2018")
        dt2.Rows.Add("Soumya", "2", "03-Jan-2018")
        dt2.Rows.Add("Soumya", "3", "04-Jan-2018")
        Me.DataGridView2.DataSource = dt2

 

Posted one year ago

Hi satabeach,

Please refer below sample.

Code

C#

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
public Form1()
{
    InitializeComponent();
    dt1.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("01-Jan-18"), new DataColumn("02-Jan-18"), new DataColumn("03-Jan-18"), new DataColumn("04-Jan-18") });
    dt1.Rows.Add("Ahemd", "1", "1", "OFF", "OFF");
    dt1.Rows.Add("Ajith", "3", "3", "2", "2");
    dt1.Rows.Add("Soumya", "OFF", "1", "1", "1");
    this.DataGridView1.DataSource = dt1;
}

private void Button1_Click(object sender, EventArgs e)
{
    dt2.Columns.Add("Name");
    dt2.Columns.Add("Duty");
    dt2.Columns.Add("Date");
    for (int row = 0; row < dt1.Rows.Count; row++)
    {
        for (int column = 1; column < dt1.Columns.Count; column++)
        {
            dt2.Rows.Add(dt1.Rows[row][0], dt1.Rows[row][column], dt1.Columns[column].ColumnName);
        }
    }
    DataGridView1.DataSource = dt2;
}

 VB.Net

Private dt1 As DataTable = New DataTable()
Private dt2 As DataTable = New DataTable()
Public Sub New()
    InitializeComponent()
    dt1.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("01-Jan-18"), New DataColumn("02-Jan-18"), New DataColumn("03-Jan-18"), New DataColumn("04-Jan-18")})
    dt1.Rows.Add("Ahemd", "1", "1", "OFF", "OFF")
    dt1.Rows.Add("Ajith", "3", "3", "2", "2")
    dt1.Rows.Add("Soumya", "OFF", "1", "1", "1")
    Me.DataGridView1.DataSource = dt1
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    dt2.Columns.Add("Name")
    dt2.Columns.Add("Duty")
    dt2.Columns.Add("Date")
    For row As Integer = 0 To dt1.Rows.Count - 1
        For column As Integer = 1 To dt1.Columns.Count - 1
            dt2.Rows.Add(dt1.Rows(row)(0), dt1.Rows(row)(column), dt1.Columns(column).ColumnName)
        Next
    Next
    DataGridView1.DataSource = dt2
End Sub

Screenshot