将列添加到DataGridView

时间:2015-08-15 12:16:53

标签: c# datagridview

我用属性填充我的列表,我希望这个列表成为dataGridviiew的数据源。

但是当我将此列表设置为dataGridView作为dataSource时,所有属性值都在一列中。如何在dataGridView中为每个属性添加一列?

这是我的代码:

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{       
    Restaurant n = new Restaurant );

    foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
    {
        IList<String> lista = new List<String>();
        n.Data = string.Empty;
        n.Data2 = string.Empty;
        int indexOfYourColumn = 9;
        int index2 = 0;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            n.Data = row.Cells[indexOfYourColumn].Value.ToString();
            n.Data2 = row.Cells[index2].Value.ToString();
            if (cell.Value.ToString() == n.Data.ToString())
            {

                lista.Add(n.Data);
                lista.Add(n.Data2);
                dataGridView3.DataSource = lista.Select(x => new { Value = x }).ToList();

            }
        }         
    }
}

0 个答案:

没有答案