I have procedure that programmatically adding list to datagridview. For Example:

public List<Color_INFO> addrowtocolors()
{    
    List<Color_INFO> result = dal.GetColor();
    for (int i = 0; i < list.Count; i++)
    {
         var index = grdColors.Rows.Add();
         grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
         grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
    }
    return null;
}

But when I call it is adding 3 same rows to datagridview , and in list i have only one.

I know that I can use dataset option , but that not fit for my needs.

Thanx.

有帮助吗?

解决方案

As per my comment... I've also removed the return value as it seemed strange to just return null. (or even the proposed list based on your function name).

public void addrowtocolors()
{    
    for (int i = 0; i < result.Count; i++)
    {
         var index = grdColors.Rows.Add();
         grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
         grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
    }
}

or

public void addrowtocolors()
{    
    for (int i = 0; i < list.Count; i++)
    {
         var index = grdColors.Rows.Add();
         grdColors.Rows[index].Cells["Code"].Value = list[i].Code.ToString();
         grdColors.Rows[index].Cells["Desc"].Value = list[i].Desc.ToString();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top