DataGridViewComboBoxCell 如何选中其中一项?

墨香斋 2006-09-13 07:58:31

DataGridViewcomboBoxCell cell = new DataGridViewcomboBoxCell();
cell.Items.Add("Apple");
cell.Items.Add("Orange");
cell.Items.Add("Banala");

请问如何选中 Orange ?

...全文
795 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hy98521 2006-09-14
  • 打赏
  • 举报
回复
给你个例子。 不知道能不能帮上忙

private void ledgerStyleButton_Click(object sender, System.EventArgs e)
{
// Create a new cell style.
DataGridViewCellStyle style = new DataGridViewCellStyle();
{
style.BackColor = Color.Beige;
style.ForeColor = Color.Brown;
style.Font = new Font("Verdana", 8);
}

// Apply the style as the default cell style.
dataGridView1.AlternatingRowsDefaultCellStyle = style;
ledgerStyleButton.Enabled = false;
}

private void SetUpDataGridView()
{
this.Controls.Add(dataGridView1);
dataGridView1.ColumnCount = 5;
DataGridViewCellStyle style =
dataGridView1.ColumnHeadersDefaultCellStyle;
style.BackColor = Color.Navy;
style.ForeColor = Color.White;
style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.Name = "dataGridView1";
dataGridView1.Location = new Point(8, 8);
dataGridView1.Size = new Size(500, 300);
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.ColumnHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Raised;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.GridColor = SystemColors.ActiveBorder;
dataGridView1.RowHeadersVisible = false;

dataGridView1.Columns[0].Name = "Release Date";
dataGridView1.Columns[1].Name = "Track";
dataGridView1.Columns[1].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].Name = "Title";
dataGridView1.Columns[3].Name = "Artist";
dataGridView1.Columns[4].Name = "Album";

// Make the font italic for row four.
dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;

dataGridView1.BackgroundColor = Color.Honeydew;

dataGridView1.Dock = DockStyle.Fill;

dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
ledgerStyleButton.Click += new EventHandler(ledgerStyleButton_Click);
dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

}

private void PopulateDataGridView()
{

// Create the string array for each row of data.
string[] row0 = { "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
string[] row1 = { "4/4/1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
string[] row2 = { "11/11/1971", "1", "One of These Days", "Pink Floyd", "Meddle" };
string[] row3 = { "4/4/1988", "7", "Where Is My Mind?", "Pixies", "Surfer Rosa" };
string[] row4 = { "5/1981", "9", "Can't Find My Mind", "Cramps", "Psychedelic Jungle" };
string[] row5 = { "6/10/2003", "13", "Scatterbrain. (As Dead As Leaves.)", "Radiohead", "Hail to the Thief" };
string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };

// Add a row for each string array.
{
DataGridViewRowCollection rows = this.dataGridView1.Rows;
rows.Add(row0);
rows.Add(row1);
rows.Add(row2);
rows.Add(row3);
rows.Add(row4);
rows.Add(row5);
rows.Add(row6);
}

// Change the order the columns are displayed.
{
DataGridViewColumnCollection columns = this.dataGridView1.Columns;
columns[0].DisplayIndex = 3;
columns[1].DisplayIndex = 4;
columns[2].DisplayIndex = 0;
columns[3].DisplayIndex = 1;
columns[4].DisplayIndex = 2;
}
}
hzh_wen_x 2006-09-14
  • 打赏
  • 举报
回复
up
qpl007 2006-09-14
  • 打赏
  • 举报
回复
DataGridViewcomboBoxCell cell = new DataGridViewcomboBoxCell();
cell.Items.Add("Apple");
cell.Items.Add("Orange");
cell.Items.Add("Banala");

Try:--

cell.Value = "Orange";
hy98521 2006-09-14
  • 打赏
  • 举报
回复
有没有完整的代码拿出来啊。
Csharp110 2006-09-14
  • 打赏
  • 举报
回复
关注中
新鲜鱼排 2006-09-13
  • 打赏
  • 举报
回复
可以将需要显示的最后添加进去。
没有环境,所以无法测试,不好意思。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧