有人能给我解释一下如何在dataGridView中添加DataGridViewComboBoxCell吗?代码是这样的:
foreach(....){
DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
//cmb.item.add(....
dataGridView.Rows.Add(new object[] {cmb,name,surname}); } 网格中的第一个单元格是DataGridViewComboBoxColumn类型,我尝试将cmb更改为DataGridViewComboBoxColumn,但仍然没有结果。
我处理了DataError,所以我没有得到“值无效”的错误,但是我在datagridview中的组合框是空的。
发布于 2014-06-01 22:53:56
好了,我解决了这个问题。看起来你必须一步一步地给单元格加值。
我将给出一般性的解释,因为似乎有很多人对此有问题。假设你有3列的DataGridView,依次是DataGridViewTextBoxCell,DataGridViewComboBoxColumn,DataGridViewCheckBoxCell。现在,你必须使用Desinger来制作这三个列,否则它将无法工作。
因此,现在您想要向网格添加特定的值,每一行表示一个人。在设计器中,它看起来像
Name PhoneNumberS Married
..*.. |.....|..............|.........|.... 所以你想要将他的名字添加到textboxcell,将电话枚举列表添加到comboboxcell,并检查checkboxcell是否结婚。并对列表中的每个人重复一遍。
下面是伪代码:
foreach(Person p in people.getAll()){
/////MAKE NEW CELL FOR EACH VALUE
DataGridViewTextBoxCell name= new DataGridViewTextBoxCell();
name.Value = p.name;
DataGridViewTextBoxCell phones= new DataGridViewTextBoxCell();
foreach(Int Pnumber in p.numbers){
phones.items.add(Pnumber);
}
DataGridViewCheckBoxCell ismarried = new DataGridViewCheckBoxCell();
ismarried.Value = p.married;
///////// MAKE NEW ROW AND ADD CELLS
DataGridViewRow row = new DataGridViewRow();
row .Cells.Add(name);
row .Cells.Add(phones);
row .Cells.Add(ismarried );
///// ADD ENTIRE ROW TO DATA GRID
dataGridView.Rows.Add(row);
}重复一遍,你首先必须使用DESIGNER将列添加到网格中,当你在代码中将单元格添加到中时,它的顺序必须与DESIGNER中看到的完全相同。
发布于 2014-06-01 15:50:56
我认为您想要添加一个DataGridViewComboBoxColumn类型的列。
DataGridViewComboBoxColumn d = new DataGridViewComboBoxColumn();
d.Name = "Drinks";
d.Items.Add("Coca-Cola");
d.Items.Add("Sprite");
dataGridView1.Columns.Add(d);我希望它能有所帮助。
发布于 2016-08-02 21:56:19
关于如何解决这个问题,有不同的方法。我发布这个答案是因为我正在处理类似的情况,并找到了一个像样的解决方案。DataGridView必须放在公式中,其余的(在本例中)是在代码中完成的。首先,我们从DataGridView的DataSource开始。
DataTable myData = new DataTable();
myDataGridView.AutoGenerateColumns = false;关闭自动生成非常重要,这样我们就可以根据需要设置列。现在向我们的DataTable添加一些数据。在此示例中,猫:
myData.Columns.Add("Name", typeof(string));
myData.Columns.Add("Gender", typeof(string));
myData.Rows.Add(new object[] {"Ser Pounce", "male"});
myData.Rows.Add(new object[] {"Fluffy", "male"});
myData.Rows.Add(new object[] {"Peach", "female"});在指定数据及其列之后,我们可以在DataGridView中创建特定的列。
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.HeaderText = "Cat name";
nameColumn.DataPropertyName = "Name";
nameColumn.ReadOnly = true;
DataGridViewComboBoxColumn genderColumn = new DataGridViewComboBoxColumn ();
List<string> genderList = new List<string>() { "male", "female", "unknown" };
genderColumn.DataSource = genderList;
genderColumn.HeaderText = "Gender";
genderColumn.DataPropertyName = "Gender";
genderColumn.ValueType = typeof(string);
myDataGridView.DataSource = myData;
myDataGridView.Columns.AddRange(nameColumn, genderColumn);在添加定义的列之前,将我们的数据作为DataGridView的DataSource添加。请注意,每列(DataGridView)的DataPropertyName必须与我们在DataTable中为列指定的名称相同。HeaderText可以使用不同的名称。最后,匹配列和数据的ValueType。
https://stackoverflow.com/questions/23975323
复制相似问题