日期:2014-05-20  浏览次数:20791 次

100分求关于datagridview动态列中的Cells
有一个DataGridView,其中有一列初始定义为DataGridViewTextBoxColumnTextBoxColumn,
则其每个Cell的type为DataGridViewComboBoxCell.

现在我想根据条件动态改变Cell,
比如:当为0时,cell变成DataGridViewComboBoxCell下拉方式.

怎么样把新定义的DataGridViewComboBoxCell加到cell中去呢?
能这样加吗   ?

------解决方案--------------------
在binddataRow事件中试试
------解决方案--------------------
为什么要这么加哦,有啥意义
------解决方案--------------------
这个实现很简单,你在使用DataGridView列编辑器添加列时,第一列选择类型DataGridViewComboBoxColumn,其余的列都选:DataGridViewTextBoxColumnColumn,

添加列后,你需要对第一列绑定数据源,以便填充它的下拉框,给你一个范例:

private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
DataGridViewComboBoxColumn column =
new DataGridViewComboBoxColumn();
{
column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString();
column.HeaderText = ColumnName.TitleOfCourtesy.ToString();
column.DropDownWidth = 160;
column.Width = 90;
column.MaxDropDownItems = 3;
column.FlatStyle = FlatStyle.Flat;
}
return column;
}

private void SetAlternateChoicesUsingDataSource(ref DataGridViewComboBoxColumn comboboxColumn)
{
{
comboboxColumn.DataSource = RetrieveAlternativeTitles();
comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString();
comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
}
}

private DataTable RetrieveAlternativeTitles()
{
return Populate( "SELECT distinct TitleOfCourtesy FROM Employees ");
}

string connectionString =
"Integrated Security=SSPI;Persist Security Info=False; " +
"Initial Catalog=Northwind;Data Source=localhost ";

private DataTable Populate(string sqlCommand)
{
SqlConnection northwindConnection = new SqlConnection(connectionString);
northwindConnection.Open();

SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;

DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);

return table;
}


------解决方案--------------------
顶,关注中!
------解决方案--------------------
路过看看...

帮顶..
------解决方案--------------------
DATAGRIDVIEW中有DATAGRIDVIEWCELLS这个类,用这个类试试
先创建这个类的实例
再进行判断
------解决方案--------------------
满BT的需求
------解决方案--------------------
Column的呈现风格是由DisplayType决定的,应用后改变Cell的EditControl不容易哦

暂想到的方法是,自己重写DataGridViewColumn增加EditControlType属性(与数据相关的条件属性),并在EditControl的UserControl做处理

关注下,hoho