WinForm DataGridViewComboCell数据绑定、事件机制实例

     关于赋值,请参照以下网址:

    http://blog.csdn.net/atlasroben/archive/2008/08/03/2762426.aspx

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

    这些都比较详细,故不再赘述。

    我写过以下一段代码可以实现。x


///

/// 数据绑定 /// /// private void DataBinding(soLayers pLayers) { DataTable dt = new DataTable(); dt.Columns.Add("图层名"); for (int i = 1; i <= pLayers.Count; i++) { DataRow dr = dt.NewRow(); dr[0] = pLayers[i].Dataset.Name; dt.Rows.Add(dr); } this.dgvClipSet.DataSource = dt.DefaultView; this.dgvClipSet.Columns[0].ReadOnly = true; this.dgvClipSet.Columns[0].Width = 100; DataGridViewCheckBoxColumn dgvCheckBox = new DataGridViewCheckBoxColumn(); dgvCheckBox.Width = 20; this.dgvClipSet.Columns.Insert(0, dgvCheckBox); DataGridViewTextBoxColumn dgvTxtBox = new DataGridViewTextBoxColumn(); dgvTxtBox.HeaderText = "裁剪到目标图层名"; dgvTxtBox.Width = 150; this.dgvClipSet.Columns.Insert(2, dgvTxtBox); DataGridViewTextBoxColumn dgvTxtEraseBox = new DataGridViewTextBoxColumn(); dgvTxtEraseBox.HeaderText = "擦除到目标图层名"; dgvTxtEraseBox.Width = 150; this.dgvClipSet.Columns.Insert(3, dgvTxtEraseBox); DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn(); dgvComboBox.HeaderText = "区域"; dgvComboBox.Width = 50; dgvComboBox.Items.Add("内"); dgvComboBox.Items.Add("外"); //dgvComboBox.Value = "内"; this.dgvClipSet.Columns.Insert(4, dgvComboBox); DataGridViewComboBoxColumn dgvComboTypeBox = new DataGridViewComboBoxColumn(); dgvComboTypeBox.HeaderText = "是否擦除"; dgvComboTypeBox.Width = 100; dgvComboTypeBox.Items.Add("是"); dgvComboTypeBox.Items.Add("否"); //dgvComboTypeBox = "否"; this.dgvClipSet.Columns.Insert(5, dgvComboTypeBox); foreach (DataGridViewRow dgvRow in this.dgvClipSet.Rows) { if (dgvRow.Cells[2].Value == null) { dgvRow.Cells[2].Value = dgvRow.Cells[1].Value.ToString() + "_1"; } if (dgvRow.Cells[3].Value == null) { dgvRow.Cells[3].Value = dgvRow.Cells[1].Value + "_2"; } if (dgvRow.Cells[4].Value == null) { dgvRow.Cells[4].Value = "内"; } if (dgvRow.Cells[5].Value == null) { dgvRow.Cells[5].Value = "否"; } } } 

      关于事件绑定机制,需要将其转成一个combobox对象来进行操作,可以实现combobox类似的功能

     ///

/// DataGridViewComboCell改变事件. /// /// /// private void dgvClipSet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (this.dgvClipSet.CurrentCell.ColumnIndex == 4) { (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(FrmOverlayAnalystClip_SelectedIndexChanged); } if (this.dgvClipSet.CurrentCell.ColumnIndex == 5) { (e.Control as ComboBox).SelectedIndexChanged+=new EventHandler(FrmOverlayAnalystClip_CmbSelectedIndexChanged); } } void FrmOverlayAnalystClip_SelectedIndexChanged(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; try { if (combox.Text == "内") { this.rdbAreaIn.Checked = true; } else { this.rdbAreaOut.Checked = true; } combox.SelectedIndexChanged -= new EventHandler(FrmOverlayAnalystClip_SelectedIndexChanged); } catch { } } void FrmOverlayAnalystClip_CmbSelectedIndexChanged(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; try { if (combox.Text == "是") { this.chkErase.Checked = true; } else { this.chkErase.Checked = false; } //combox.SelectedIndexChanged -= new EventHandler(FrmOverlayAnalystClip_CmbSelectedIndexChanged); } catch { } } 

你可能感兴趣的:(winform,object,null)