Click here to Skip to main content
15,881,139 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.

C#
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];
                    dgvHeader.Rows.Add();
                    itemCount++;
                }


What I have tried:

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

1 solution

Try this:
C#
//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('-');
    dt.Rows.Add(itemArray);
}
//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[^]
 
Share this answer
 
v2
Comments
BillWoodruff 19-Mar-21 16:23pm    
+5
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