DataGridView C#使用MVP Pattern为不同的行添加不同的值到Combobox [英] DataGridView C# Add Different Values Into a Combobox for each row using MVP Pattern
问题描述
我目前正在尝试从层(我的类)对象的列表中的值添加到datagridview。第一列只是一个具有文本的常规单元格,但我想要第二列包括带有值的每个行的组合框。我在我的演示者中有一个foreach循环,循环遍历对象列表,并调用方法AddRow,传递它的名称值和样式列表。这是我到目前为止。标题列表很好,但是组合框中不存在样式。我如何填充一个组合框中的值列表内为创建的每一行?由于公司政策我不能张贴屏幕截图,所以我会尽我所能回答任何歧义,谢谢。
I am currently trying to add values from a list of Layer(my class) objects to a datagridview.The first column is just a regular cell with text, but I want the second column to include comboboxes with values for each row. I have a foreach loop in my presenter that loops through the list of objects and calls the method AddRow, passing it the name value and the styles list. This is what I got so far. The list of titles are being brought in fine, but the styles are not there for the comboboxes. How can I populate a combobox with the values inside of the list for each row that is created? Due to company policy I can't post a screenshot so I'll do my best to answer any ambiguities, thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Service
{
public class Layer
{
public string Name { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public bool Selected { get; set; }
public List<string> CRS = new List<string>();
public List<string> Styles = new List<string>();
}
}
//
//Method Inside of presenter to provide datagridview with values
public void ProvideLayers()
{
if (_lview != null && _layers != null)
{
foreach (Layer lay in _layers)
{
if (lay.Styles.Count == 0)
{
_lview.AddRow(lay.Title,new List<string>());
continue;
}
else
{
_lview.AddRow(lay.Title,lay.Styles);
}
}
}
}
//
public void AddRow(string lName,List<string> lStyles)
{
dataGridView1.Rows.Add(lName,lStyles);
}
推荐答案
数据源。
public void AddRow(string lName,List<string> lStyles)
{
var dgvRow = new DataGridViewRow();
dgvRow.Cells.Add(new DataGridViewTextBoxCell());
dgvRow.Cells.Add(new DataGridViewComboBoxCell());
dgvRow.Cells[0].Value = lName;
((DataGridViewComboBoxCell) dgvRow.Cells[1]).DataSource = lStyles;
dataGridView1.Rows.Add(dgvRow);
}
注意:如果您尚未设置列,强制你定义DataGridView的列如下:
Note: If you're not setting up your columns already, doing this will force you to define the DataGridView's columns like so:
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView1.Columns.Add(new DataGridViewComboBoxColumn());
这篇关于DataGridView C#使用MVP Pattern为不同的行添加不同的值到Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!