簡體   English   中英

Datagridview ComboBoxCell設置默認值嗎?

[英]Datagridview ComboBoxCell set default value?

我有一個包含大量數據的datagridview,當我添加新行時,第一列的最后一行將創建一個包含四個項目的新ComboBoxCell。 但是我無法為組合框設置默認值(“ DropDown”)。 每次我必須手動選擇“ DropDown”。 解決辦法是什么?

 DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
 dgv[1, dgv.Rows.Count - 1] = dgvCell;

 string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
 dgvCell.DataSource = controltype;
private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        e.Row.Cells[4].Value = "DropDown";
    }

這很容易,如果您在DataGrid視圖中有一個ComboBox列,並且想知道組合框的選定索引是什么,那么您需要執行以下操作:1.處理DataGrid視圖的EditingControlShowing事件。 在此事件處理程序中,檢查當前列是否與我們有關。 然后,我們創建一個臨時的ComboBox對象並獲取選定的索引:

private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}

void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}

嘗試:

if(!isPostBack)
  {
   dgvCell.SelectedItem=controltype[0].toString(); 
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM