在datagridviewcomboboxcolumn C#中的Form Load上显示默认选择的值 [英] Display default selected value on Form Load in datagridviewcomboboxcolumn C#

查看:58
本文介绍了在datagridviewcomboboxcolumn C#中的Form Load上显示默认选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview中有一个datagridviewcomboboxcolumn,我希望在表单加载/显示时默认显示在datagridviewcomboboxcolumn中选择的Item2。



我的数据库中有3个项目表记录



id名称

1项目1

2项目2

3项目3



如果有人有任何想法,请帮助我。

提前致谢。

解决方案

你可以使用单元格格式化事件,如下例所示: -

 DataGridViewComboBoxColumn cmbCol; 
private void Form7_Load(object sender,EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(c1,typeof(int));
dt.Columns.Add(c2);
for(int j = 0; j< 5; j ++)
{
dt.Rows.Add(j,c+ j.ToString());
}
DataTable dt2 = new DataTable();
dt2.Columns.Add(c1,typeof(int));
dt2.Columns.Add(c2);
for(int j = 5; j< 10; j ++)
{
dt2.Rows.Add(j,c+ j.ToString());
}
this.dataGridView1.DataSource = dt;
cmbCol = new DataGridViewComboBoxColumn();
cmbCol.DisplayMember =c2;
cmbCol.ValueMember =c1;
//cmb.DataPropertyName =c1;
cmbCol.DataSource = dt2;
this.dataGridView1.Columns.Add(cmbCol);
this.dataGridView1.CellFormatting + = new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex == 2)
{
DataTable dt = this。 cmbCol.DataSource as DataTable;
e.Value = dt.Rows [0] [c2];
}
}
}


您好,您可以使用CurrentCell Properties



 dataGrid.CurrentCell = dataGrid [columnIndex,rowIndex]; 





希望有帮助


I have a datagridviewcomboboxcolumn in datagridview and I want to display Item2 as selected in datagridviewcomboboxcolumn by default when form loads/displayed.

I have 3 items in my database table Records

id Name
1 Item1
2 Item2
3 Item3

If somebody have any idea please do help me.
Thanks in advance.

解决方案

you can use cell formatting event like in the below case :-

DataGridViewComboBoxColumn cmbCol;
private void Form7_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("c1",typeof(int));
dt.Columns.Add("c2");
for (int j = 0; j < 5; j++)
{
dt.Rows.Add(j,"c"+j.ToString());
}
DataTable dt2 = new DataTable();
dt2.Columns.Add("c1", typeof(int));
dt2.Columns.Add("c2");
for (int j = 5; j < 10; j++)
{
dt2.Rows.Add(j, "c" + j.ToString());
}
this.dataGridView1.DataSource = dt;
cmbCol = new DataGridViewComboBoxColumn();
cmbCol.DisplayMember = "c2";
cmbCol.ValueMember = "c1";
//cmb.DataPropertyName = "c1";
cmbCol.DataSource = dt2;
this.dataGridView1.Columns.Add(cmbCol);
this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
DataTable dt = this.cmbCol.DataSource as DataTable;
e.Value = dt.Rows[0]["c2"];
}
}
}


Hi you may use CurrentCell Properties

dataGrid.CurrentCell = dataGrid[columnIndex, rowIndex];



Hope it helps


这篇关于在datagridviewcomboboxcolumn C#中的Form Load上显示默认选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆