Click here to Skip to main content
15,390,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys need a little help,i'm splitting 27 items then add them one by one
in two columns, but my problem is the result of this code was 26 blank row in
datagridview and i only get the last data. where did i go wrong. Kindly point out
my mistake please.

int itemCount = 0;
                foreach (string item in getBaplieDetailsResult)
                    string itemResult = item;
                    string[] itemArray = itemResult.Split('-');
                    dgvHeader.Rows[itemCount].Cells[0].Value = itemArray[0];
                    dgvHeader.Rows[itemCount].Cells[1].Value = itemArray[1];

What I have tried:

i'm still searching the google for now..
Updated 18-Mar-21 21:59pm

1 solution

Try this:
//set below property via code or manually
//dgvHeader.AutoGenerateColumns = true;
//create datatable object
DataTable dt = new DataTable();
//add columns
dt.Columns.AddRange(new DataColumn[]
    new DataColumn("Column#1", typeof(string)),
    new DataColumn("Column#2", typeof(string))
//add rows
foreach (string item in getBaplieDetailsResult)
    string[] itemArray = item.Split('-');
//bind ddata to datagridview
dgvHeader.DataSource = dt;

For further details, please see:
Bind data to DataGridView Control - Windows Forms .NET Framework | Microsoft Docs[^]
Create an Unbound DataGridView Control - Windows Forms .NET Framework | Microsoft Docs[^]
BillWoodruff 19-Mar-21 16:23pm
Maciej Los 19-Mar-21 16:25pm
Thank you very much, Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900