user2644434
user2644434

Reputation: 1

DataGridView cannot display data c#

I try to display data in the DataGridView in C# but it can't show anything at all. My DataGridView is inside a private static void function.

private static void processsub(Event a , Session session)
{ 
   List<String[]> lista = new List<string[]>();
   lista.add(new string[] {text1, text2, text3, text4, text5, text6, text7 });
   Form1 frm1 = newForm1();
   frm1.dataGridView1.AutoGenerateColumns=true;
   DataTable table = ConvertListtoDatTable(lista);
   frm1.dataGridview1.DataSource = table;
}

I used the messagebox window to display the " .Row[0].Cell[0].Value.ToString()" and it shows the right value but the DataGridView just didn't display the data.

Anyone have any suggestion?

Upvotes: 0

Views: 1774

Answers (1)

user2492798
user2492798

Reputation: 589

You may not spread rows.My test code is as the following.

   private void processsub()
    {
        List<String[]> lista = new List<string[]>();
        lista.Add(new string[] { "text1", "text2", "text3", "text4", "text5", "text6", "text7" });
        dataGridView1.AutoGenerateColumns = true;
        DataTable table = ConvertListtoDatTable(lista);
        dataGridView1.DataSource = table;
    }

    private DataTable ConvertListtoDatTable(List<String[]> lista)
    {
        DataTable table = new DataTable();
        int columnCount = lista[0].Length;
        for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
        {
            table.Columns.Add();
        }
        foreach (string[] row in lista)
        {
            table.Rows.Add(row);
        }
        return table;
    }

    private void buttonTest_Click(object sender, EventArgs e)
    {
        processsub();
    }

Execute result: enter image description here

Upvotes: 0

Related Questions