2011-03-21 71 views
7

如何改變DataGridViewComboBoxColumn下面的事情在運行時:運行時禁用datagridviewcombobox

  1. 如何設置組合框爲默認的第一個值?
  2. 禁用組合框/在將第一個值顯示爲默認值時將其設置爲只讀。意思是說,如果我在組合框中有3個項目,它應該只顯示第一個項目。 (可以禁用組合框下拉菜單,或將其更改爲運行時的文本框)。

原因:
我這樣做的原因是因爲我Enum我有Status{New=1,Stop=2,Temp=3}。當我想註冊學生時,狀態始終設置爲New。所以當我保存時,它會自動保存Status = 1

回答

8

下面是如何設置的默認值,禁用電池:

using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication3 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnLoad(EventArgs e) 
     { 
      base.OnLoad(e); 

      Column1.DataSource = new int[] { 1, 2, 3 }; 
      Column1.DataPropertyName = "Number"; 
      dataGridView1.DataSource = new[] 
      { 
       new { Number=1 }, 
       new { Number=2 }, 
       new { Number=3 }, 
       new { Number=1 } 
      }; 
     } 

     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
     { 
      if (e.ColumnIndex == Column1.Index && e.RowIndex == (dataGridView1.Rows.Count - 1)) 
      { 
       DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex]; 

       cell.Value = 2; 
       cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; 
       cell.ReadOnly = true; 
      } 
     } 
    } 
} 
+0

@Josh M .:還沒有啓用屬性((DataGridViewComboBoxColumn)dg.Columns [「Status」])。En ... – VeecoTech 2011-03-21 03:44:09

+0

我們是不是在談論同一個控件?此控件具有我提到的兩個屬性:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx – 2011-03-21 03:56:57

+0

哦! DataGridViewComboBoxColumn!好吧,讓我檢查它... – 2011-03-21 03:57:32

0

存在的SelectedIndex爲DataGridView的組合框控件每這些文章:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol(v=vs.80).aspx

[1]。這就是我要做的事:

private void dgv_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e) 
{ 
if (e.Control is DataGridViewComboBoxEditingControl) { 


    DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl; 


    BindingSource bs = control.DataSource as BindingSource; 
    if (!IsNothing(bs)) { 

     // set the filteredChildBS as the DataSource of the editing control 
     ((ComboBox)e.Control).DataSource = filteredChildBS; 

     //Set the dgv's combobox to the first item 
     ((ComboBox)e.Control).SelectedIndex = 1 

    } 
} 

}

的filtereredChildBS是BindingSource的,讓我知道如果你需要任何澄清?

[2]。禁用datagridview控制它有點棘手。我用這個例子來禁用DataGridView的複選框:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/988c7e3f-c172-467d-89b7-b80a60b7f24f/但對於組合框一個簡單的方法是禁用列樂隊:

foreach (DataGridViewBand band in dgvTransactions.Columns) { if (i !=7) band.ReadOnly = (bool)i == 0;  i+= 1; band.Frozen = false; } 

讓我知道如果你需要進一步澄清?

+0

感謝您的答覆。但是什麼是C#中的TryCast?我無法在visualstudio framekwork中找到或寫入TryCast 3.5 – VeecoTech 2011-03-21 05:45:05

+0

對不起,我使用CodeChanger.com從VB.Net移植過來,並填滿了投射。 ((ComboBox)e.Control).SelectedIndex – 2011-03-21 05:51:02

+0

什麼是filteredChildBS?我應該加載所有的枚舉? – VeecoTech 2011-03-21 06:43:13