如何将值添加到具有ComboBox列的DataGrid

时间:2014-01-15 00:35:57

标签: c# wpf linq datagrid combobox

我要做的是收集DocNumber引用的数据库并将其放在DataGrid中,记录有时可以是单个到多个。依赖于检索所需的方式。这是我的功能代码如下:

internal void SelectDetailsRecord(string p) 
{
    SFCDataContext SFC = new SFCDataContext();
    try
    {
        var DetailsRec = SFC.Sales_OrderFormDetails.Where(w => w.OrderDocNum == p)
            .Select(t => new { 
                    Ord = t.OrderDocNum,
                    Line = t.LineNumber,
                    Vcode = t.VariantCode,
                    Vdesc = t.VariantDesc,
                    Icode = t.Itemnmbr,
                    Idesc = t.ItemDesc,
                    Qty = t.Quantity,
                    UofM = t.UofM,
                    Kgs = t.KgsPerBag,
                    Post = t.Posted
            });
        int count = 0;
        foreach (var r in DetailsRec) 
        {
            decimal TotalKgs = Convert.ToDecimal(r.Qty) * Convert.ToDecimal(r.Kgs);
            string[] row = 
            { 
                r.Qty.ToString(), 
                r.Icode, 
                r.Idesc, 
                r.UofM, 
                r.Vcode, 
                r.Vdesc, 
                r.Kgs.ToString(), 
                TotalKgs.ToString(), 
                r.Line.ToString() 
            };

            //DataGridView1.Rows.Add(row); <-- Tried this one but returns me an error statement.

            DataGridView1.Rows[count].Cells[0].Value = row[0];
            DataGridView1.Rows[count].Cells[1].Value = row[1]; <-- this Part here is the combo box column
            DataGridView1.Rows[count].Cells[2].Value = row[2];
            DataGridView1.Rows[count].Cells[3].Value = row[3];
            DataGridView1.Rows[count].Cells[4].Value = row[4];
            DataGridView1.Rows[count].Cells[5].Value = row[5];
            DataGridView1.Rows[count].Cells[6].Value = row[6];
            DataGridView1.Rows[count].Cells[7].Value = row[7];
            DataGridView1.Rows[count].Cells[8].Value = row[8];

            count++;
        }                
    }
    catch (Exception) { }
    SFC.Connection.Close();
}

有没有办法可以将记录的值放入datagrid组合框中,就像在这样的组合框中放置一个值:“ComboBox.Text = Value;”如果anytone有想法,请提供帮助。

2 个答案:

答案 0 :(得分:0)

为了使用项目列表填充ComboBox,您需要设置其DataSource。

myComboBox.DataSource = myList;

请参阅相关的MSDN页面:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox%28v=vs.110%29.aspx

答案 1 :(得分:0)

此代码必须解决此问题:

 DataGridView1.Rows[count].Cells[1].Value = Convert.ToString(row[1]);

您必须将object值转换为string值才能设置GridComboBox中的值。同时确保集合row[1]

中存在GridComboBox的值