大家好,您如何制作第一行包含文本而第二行包含组合框的网格视图?
我尝试了下面的代码,但它将组合框控件渲染成一个简单的文本,说明组合框的总项目。
// create columns
DataColumn column1 = new DataColumn();
column1.Caption = "Attribute";
column1.ColumnName = "Attribute";
column1.DataType = typeof(string);
DataColumn column2 = new DataColumn();
column2.Caption = "Value";
column2.ColumnName = "Value";
column2.DataType = typeof(object);
DataTable dt = new DataTable();
dt.Columns.Add(column1);
dt.Columns.Add(column2);
// populate field
DataRow row1 = dt.NewRow();
row1.ItemArray = new object[] { "Id", "1" };
DataRow row2 = dt.NewRow();
row2.ItemArray = new object[] { "Name", "Vincent" };
ComboBox cbox = new ComboBox();
cbox.DropDownStyle = ComboBoxStyle.DropDownList;
cbox.Items.AddRange(new object[]{1,2,3,4,5});
DataRow row3 = dt.NewRow();
row3.ItemArray = new object[] { "Num of Siblings", cbox};
DataRow row4 =dt.NewRow();
row4.ItemArray = new object[] { "Age", "21" };
dt.Rows.Add(row1);
dt.Rows.Add(row2);
dt.Rows.Add(row3);
dt.Rows.Add(row4);
// populate to datagridview
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
还有其他方法吗?我也可以将组合框与datagridview控件分开,就像点击单元格(应该是组合框)一样,它会弹出一个带有组合框的对话框(第二种形式),但这会让用户填充不舒服。
用户要求我拥有这种功能,它应该在datagridview中(字段是从xml(动态字段)获得的)。下拉列表的内容只是一个示例,它将被赋予不同的值,例如输入性别时,它将包含男性和女性项目。
提前致谢。
答案 0 :(得分:0)
尝试将此文本框和组合框添加到同一列但不同的行See reference
中 dataGridView1.ColumnCount = 2; // Create 2 text box columns
dataGridView1.Columns[0].HeaderText = "Name";
dataGridView1.Columns[0].Width = 350;
dataGridView1.Columns[1].HeaderText = "Value";
dataGridView1.Columns[1].Width = 150;
DataGridViewRow heterow0 = new DataGridViewRow();
DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();
textBoxCell.Value = "Turn Over Based On";
DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
comboCell.Items.Add("Chip Transaction");
comboCell.Items.Add("Win/Loss");
heterow0.Cells.Add(textBoxCell);
heterow0.Cells.Add(comboCell);
dataGridView1.Rows.Add(heterow0); // this row has a combo in first cell
答案 1 :(得分:0)
如果你的意思是......
column1 column2 column3
row1:combobox1,combobox2,combobox2,.....
row2: textbox1, textbox2, textbox3,......
然后这样做
List<string> newlist = new List<string>() { "12", "34", "56", "78", "90" };
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken;
dataGridView1.ColumnCount = newlist.Count;
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
c.HeaderText = "yourvalue";
c.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
c.DefaultCellStyle = new DataGridViewCellStyle() { BackColor = Color.AntiqueWhite, Alignment = DataGridViewContentAlignment.MiddleCenter };
}
DataGridViewRow dgr = new DataGridViewRow();
DataGridViewRow dgr1 = new DataGridViewRow();
//DataGridViewColumnCollection dgcc = new DataGridViewColumnCollection(dataGridView1);
foreach (string s in newlist)
{
DataGridViewTextBoxCell tc = new DataGridViewTextBoxCell();
DataGridViewComboBoxCell cc = new DataGridViewComboBoxCell();
cc.Sorted = true;
cc.Items.Add(s);
tc.Value = s;
dgr.Cells.Add(cc as DataGridViewCell);
dgr1.Cells.Add(tc);
}
dataGridView1.Rows.AddRange(new DataGridViewRow[] { dgr, dgr1 });
在我的示例中,我使用了一个列表,但您可以使用您需要的内容,请记住,foreach将字符串分配给每个单元格用于演示,您可以根据需要添加值。