DataGridView 使用問題

來源:互聯網
上載者:User

1.除非 DataGridView 被資料繫結到支援更改通知並允許刪除的 IBindingList,否則不能以編程方式移除行

  資料來源中刪除資料,重新整理顯示控制項。

2.System.ArgumentException:DataGridViewComboBoxCell值無效,要替換此預設對話方塊,請處理DataError事件

  需要在事件中處理:

Code
 private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)
        {
           if((e.ColumnIndex ==2||e.ColumnIndex ==3)&& e.Exception is ArgumentException)
           {
               e.Cancel = true;
           }
        }

原因:列中存在空資料項目,和ComboBox項不匹配。

 

3.DataGridView 綁定枚舉

Code
 1  DataGridViewColumn kXBDataGridViewComboBoxColumn = CreateComboBoxWithEnums();
 2             kXBDataGridViewComboBoxColumn.DataPropertyName = "KXB";
 3             kXBDataGridViewComboBoxColumn.HeaderText = "性別";
 4             kXBDataGridViewComboBoxColumn.Name = "kXBDataGridViewComboBoxColumn";
 5             kXBDataGridViewComboBoxColumn.ReadOnly = true;
 6             kXBDataGridViewComboBoxColumn.Resizable = DataGridViewTriState.True;
 7             kXBDataGridViewComboBoxColumn.Width = 60;
 8             dataGridView1.Columns.Add(kXBDataGridViewComboBoxColumn);
 9 
10 
11     static DataGridViewComboBoxColumn CreateComboBoxWithEnums()
12         {
13             DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
14             combo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
15             combo.DataSource = Enum.GetValues(typeof(EnumGender));
16             string[] names = Enum.GetNames(typeof(EnumGender));
17             Dictionary<string, int> dictionary = new Dictionary<string, int>();
18             foreach (string name in names)
19             {
20                 EnumGender parse = (EnumGender)Enum.Parse(typeof(EnumGender), name);
21                 dictionary.Add(EnumDescription.GetEnumDescription(parse), (int)parse);
22             }
23             combo.DataSource = new BindingSource(dictionary, null);
24             combo.DisplayMember = "Key";
25             combo.ValueMember = "Value";
26             return combo;
27         }

4.RowPrePaint 事件,System.ArgumentException: DataGridViewComboBoxCell 值無效Code
 1 int index = this.dataGridView1.Rows.Add();
 2 DataGridViewRow row = this.dataGridView1.Rows[index];
 3 DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
 4 if (cell != null)
 5 {
 6 int tt = 1;
 7 cell.Items.Add("string" ( tt).ToString());
 8 cell.Items.Add("string" ( tt).ToString());
 9 cell.Items.Add("string" ( tt).ToString());
10 cell.Items.Add("string" ( tt).ToString());
11 cell.Items.Add("string" ( tt).ToString());
12 }


dataGridView1[4, 2].Value = ((DataGridViewComboBoxCell)dataGridView1[4, 2]).Items[0].ToString();
設定第2行第5列的預設值為ComboBox中的第一項(Items[0]),此列為DataGridViewComboBoxCell。 

態度決定一切,技術改變生活

作者:winzheng                                                                                                                                               
出處:http://cnblogs.com/winzheng                                                                                                      
本文為原創作品,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權利。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.