简体   繁体   中英

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?

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM