private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[dgv.CurrentCell.ColumnIndex].Name == "Crop" && dgv.CurrentCell.RowIndex > -1)
{
DataGridViewComboBoxCell cell = dgv.Rows[dgv.CurrentCell.RowIndex].Cells["CropType"] as DataGridViewComboBoxCell;
if (cell.Value != null && cell.Value.ToString() != "")
{
string type = cell.Value.ToString();
string sqlfliter = " select CropType as PCROP from t_croptype where Type='" + type + "'";
DataTable dtf = TuiDBClass.ExecDataTable(sqlfliter);
if (dtf != null && dtf.Rows.Count > 0)
{
(e.Control as ComboBox).DataSource = dtf;
(e.Control as ComboBox).DisplayMember="PCROP";
(e.Control as ComboBox).ValueMember="PCROP";
(e.Control as ComboBox).BackColor = Color.White;
(e.Control as ComboBox).ForeColor = Color.Blue;
}
}
}
}
其中 (e.Control as ComboBox).DataSource = dtf;
(e.Control as ComboBox).DisplayMember="PCROP";
(e.Control as ComboBox).ValueMember="PCROP";
(e.Control as ComboBox).BackColor = Color.White;
(e.Control as ComboBox).ForeColor = Color.Blue; 就是你要的控件
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[dgv.CurrentCell.ColumnIndex].Name == "Crop" && dgv.CurrentCell.RowIndex > -1)
{
DataGridViewComboBoxCell cell = dgv.Rows[dgv.CurrentCell.RowIndex].Cells["CropType"] as DataGridViewComboBoxCell;
if (cell.Value != null && cell.Value.ToString() != "")
{
string type = cell.Value.ToString();
string sqlfliter = " select CropType as PCROP from t_croptype where Type='" + type + "'";
DataTable dtf = TuiDBClass.ExecDataTable(sqlfliter);
if (dtf != null && dtf.Rows.Count > 0)
{
(e.Control as ComboBox).DataSource = dtf;
(e.Control as ComboBox).DisplayMember="PCROP";
(e.Control as ComboBox).ValueMember="PCROP";
(e.Control as ComboBox).BackColor = Color.White;
(e.Control as ComboBox).ForeColor = Color.Blue;
}
}
}
}
其中 (e.Control as ComboBox).DataSource = dtf;
(e.Control as ComboBox).DisplayMember="PCROP";
(e.Control as ComboBox).ValueMember="PCROP";
(e.Control as ComboBox).BackColor = Color.White;
(e.Control as ComboBox).ForeColor = Color.Blue; 就是你要的控件