我试图为我设置为ComboBoxcolumn的datagridview列之一动态设置自定义下拉列表。当我运行应用程序时,我看到单元格中有值。但是,我看不到用户界面中弹出任何值。
field
当我连接调试器并检查((DataGridViewComboBoxCell)_iprDataGridView.Rows [i] .Cells [16])。DataSource = dt;我的项目集中有值,只是不会显示。
有关如何设置datagridview的更多代码。
private void CustomComboBoxColumns(string filter)
{
DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();
DataTable dt;
ComboBoxColumn.HeaderText = "category";
ComboBoxColumn.DataPropertyName = "category";
ComboBoxColumn.ReadOnly = false;
ComboBoxColumn.MaxDropDownItems = 100;
ComboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
ComboBoxColumn.FlatStyle = FlatStyle.Flat;
ComboBoxColumn.ValueMember = "category";
ComboBoxColumn.DisplayMember = "category";
_iprDataGridView.Columns.Insert(16, ComboBoxColumn);
for (int i = 0; i < _iprDataGridView.Rows.Count; i++)
{
dt = GetDataForCategory(filter);
((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt;
}
}
private DataTable GetDataForCategory(string filter)
{
DbConnection db = new DbConnection();
string connString = db.BuildConnectionString();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
string query = "Select category from cd_category where category like '%" + filter + "%' order by category";
using (SqlConnection conn = new SqlConnection(connString))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(ds, "category");
dt = ds.Tables["category"];
da.Dispose();
conn.Close();
}
return dt;
}
我在考虑ConfigureTextBoxColumns()之后,将创建将遍历datagridview的方法,并创建一个新的类别comboboxcolumn并根据另一个列的值插入一个新的comboboxcell。
这是有关如何为datagridview生成列的代码
_iprDataGridView.DataSource = _dbHelper.Select(queryBuild.ToString(), parameters, dbConnection);
if (_iprDataGridView.DataSource == null) return;
// In order to display and match the PIR table values, that are requried to be in
// a comboBox menu, we need to remove the columns that have been populated and then
// re-add them as comboBoxs
ConfigureDataGridView();
ConfigureComboBoxColumns();
ConfigureTextBoxColumns();
我注释了他们的类别列,以插入我自己的类别。我正在更新这个不是我开发的应用程序。
答案 0 :(得分:0)
您似乎正在尝试将数据绑定到控件。设置源之后,必须将其绑定。查看我所做的编辑。该链接提供了更多信息,并提供了一个示例 https://support.microsoft.com/en-us/help/307860/asp-net-data-binding-overview
private void CustomComboBoxColumns(string filter)
{
DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();
DataTable dt;
ComboBoxColumn.HeaderText = "category";
ComboBoxColumn.DataPropertyName = "category";
ComboBoxColumn.ReadOnly = false;
ComboBoxColumn.MaxDropDownItems = 100;
ComboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
ComboBoxColumn.FlatStyle = FlatStyle.Flat;
ComboBoxColumn.ValueMember = "category";
ComboBoxColumn.DisplayMember = "category";
_iprDataGridView.Columns.Insert(16, ComboBoxColumn);
for (int i = 0; i < _iprDataGridView.Rows.Count; i++)
{
dt = GetDataForCategory(filter);
((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt;
((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataBind(); //this edit
}
}
我也强烈建议学习使用参数化查询来防止sql注入