简体   繁体   中英

How can I add more than one column on DataGridView using string array?

I have a DataGridView and I want to fill it with two different arrays. With my current code, I can only add one column to the DataGridView . If I try to add two arrays only the last one is displayed on the DataGridView .

DataTable info= new DataTable();
            SqlDataAdapter db = new SqlDataAdapter(query, con);
            db.Fill(info);
            if (info.Rows.Count > 0)
            {
                string prods= info.Rows[0]["prods"].ToString();
                string prices= info.Rows[0]["prices"].ToString();
                string[] f1 = prods.Split(',');
                string[] f2 = prices.Split(',');


                dataGridView2.AutoGenerateColumns = true;
                dataGridView2.DataSource = f1.Select(x => new { prods= x }).ToList();
                dataGridView2.DataSource = f2.Select(y => new { prices= z }).ToList();                    
            }

So why not just add the products as the datasource and then add a column for price which you can then populate:

dataGridView2.Columns.Add(new DataColumn("Prices", typeof(string)));

int i = 0; 

foreach (string row in prods){

    dataGridView2["Prices][i] = row;
    i++;
}

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