6

我正在苦苦挣扎DataGridViewComboBoxCell。在某些情况下(比方说,事件)我必须ComboBox在我的表单中预先选择一个值DataGridView。当用户更改一个框时,我可以像这样以编程方式更改另一个框:

var item = ItemName.Items.GetListItem(name);
if (item != null)
{
    _loading = true; // that's needed to come around another CellValueChanged events
    itemView.Rows[e.RowIndex].Cells["ItemName"].Value = item;
    _loading = false;
}

ItemName.Items这样填充:

foreach (var item in _model.DefaultData.ItemList)
{
    if (item.Value.Code.HasValue()) ItemCode.Items.Add(new ListItem(item.Key, item.Value.Code));
    ItemName.Items.Add(new ListItem(item.Key, item.Value.Name));
}

获取列表项方法:

public static ListItem GetListItem(this DataGridViewComboBoxCell.ObjectCollection col, string name)
{
    ListItem retItem = null;
    foreach (ListItem item in col)
    {
        if (item.Name == name) { retItem = item; break; }
    }
    return retItem;
}

效果很好,但是...

现在我想像这样在表单加载中添加行DataGridView

foreach (var item in _model.SelectedItems)
{
    var row = new DataGridViewRow();
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
    row.Cells.Add(new DataGridViewComboBoxCell { Value = ItemCode.Items.GetListItem(item.Code) });
    row.Cells.Add(new DataGridViewComboBoxCell { Value = ItemName.Items.GetListItem(item.Name) });
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
    ...
    itemView.Rows.Add(row);
}

这引发了心爱的DataGridViewComboBox value is not valid异常。请帮忙,我对此没有想法。我不想使用DataSource或类似的东西。ItemCodeItemName列项通过 . 正确填充和返回GetListItem()。我不明白为什么它可以正常工作,但是在表单加载时它不起作用(显示它也不起作用)。

编辑:对不起,忘记添加。

我的 ListItem 类:

public class ListItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ListItem(int sid, string sname)
    {
        Id = sid;
        Name = sname;
    }
    public override string ToString()
    {
        return Name;
    }
}

我已经把它也放在表单加载上:

ItemName.ValueMember = "Id";
ItemName.DisplayMember = "Name";
ItemCode.ValueMember = "Id";
ItemCode.DisplayMember = "Name";
4

3 回答 3

5

好的,我设法自己解决了。

DataGridViewComboBoxColumn.Items显然,仅仅包含可能的项目是不够的。DataGridViewComboBoxCell.Items如果您以编程方式添加新行,您还必须添加项目。

因此,如果其他人试图使用我没有数据绑定之类的方法DataTable,这是我的解决方案:

foreach (var item in _model.SelectedItems)
{
    var row = new DataGridViewRow();
    var codeCell = new DataGridViewComboBoxCell();
    codeCell.Items.AddRange(ItemCode.Items);
    codeCell.Value = ItemCode.Items.GetListItem(item.Code);
    var nameCell = new DataGridViewComboBoxCell();
    nameCell.Items.AddRange(ItemName.Items);
    nameCell.Value = ItemName.Items.GetListItem(item.Name);
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
    row.Cells.Add(codeCell);
    row.Cells.Add(nameCell);
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Quantity });
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceLt });
    row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceEu });
    itemView.Rows.Add(row);
}
于 2014-08-26T15:17:25.007 回答
0
        DataGridViewColumn[] dgCol = 
        {
          new DataGridViewTextBoxColumn() 
          {
              HeaderText = "Conviction", 
              SortMode = DataGridViewColumnSortMode.NotSortable,
              Width = Convert.ToInt32(0.25f * grdConvictions.Width - 19)
          },
          new DataGridViewTextBoxColumn() 
          {
              HeaderText = "Points"    , 
              SortMode = DataGridViewColumnSortMode.NotSortable,
              Width = Convert.ToInt32(0.125f * grdConvictions.Width - 19)
          },
          new DataGridViewTextBoxColumn() 
          {
              HeaderText = "Date", 
              SortMode = DataGridViewColumnSortMode.NotSortable,
              Width = Convert.ToInt32(0.125f * grdConvictions.Width - 19),
          },
          new DataGridViewComboBoxColumn () 
          {

              HeaderText = "Galletas", 
              Width = Convert.ToInt32(0.25 * grdConvictions.Width - 19),          
              DataPropertyName  = "Galletas",
              DataSource = new List<String>{"",
                                          "Maiz",
                                          "Pera",
                                          "Manzana",
                                          "Sandia",
                                          "Fresa",
                                          "Melon",
                                          "Melocoton",
                                          "Maracuya",
                                          "Cereza",
                                          "Frambuesa",
                                          "Mora",
                                          "Kiwi",
                                          "Anona",
                                          "Guayaba"
                                        },
              SortMode = DataGridViewColumnSortMode.NotSortable,
              MaxDropDownItems = 10,

              DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
              ReadOnly = false
          }
        };

YourDataGridView.Columns.AddRange(dgCol);

于 2014-10-06T23:27:51.937 回答
0

注意:DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox 以查看组合。我用作“无”,并且...:

    private void grdConvictions_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        int colIndex = e.ColumnIndex;
        int rowIndex = e.RowIndex;

        if (rowIndex < 0 || colIndex < 0)
        {
            return;
        }

        DataGridView grd = (DataGridView)sender;

        if (grd[e.ColumnIndex, e.RowIndex].GetType().Name != "DataGridViewComboBoxCell")
        {
            ((DataGridViewComboBoxCell)grd.CurrentCell).DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
        }

}

    private void grdConvictions_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        int colIndex = e.ColumnIndex;
        int rowIndex = e.RowIndex;

        if (rowIndex < 0 || colIndex < 0)
        {
            return;
        }

        DataGridView grd = (DataGridView)sender;

        if (grd[e.ColumnIndex, e.RowIndex].GetType().Name != "DataGridViewComboBoxCell")
        {
            ((DataGridViewComboBoxCell)grd.CurrentCell).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
        }

    }
于 2014-10-06T23:33:19.347 回答