在我的datagridview中,我在
winforms.But中有一个textBoxcolumn和一个可编辑的组合框列,同时在组合框文本中键入新值并按下回车键,我没有得到键入的值作为相应的单元格值.有人可以帮助这个.
private void dgv_customAttributes_CellEndEdit(object sender,DataGridViewCellEventArgs e) { DataGridViewRow row = dgv_customAttributes.CurrentRow; if (row.Cells[1].Value.ToString() != null) { //Here the selectedVal is giving the old value instead of the new typed text string SelectedVal = row.Cells[1].Value.ToString(); foreach (CustomAttribute attribute in customAttributes) { if (row.Cells[0].Value.ToString() == attribute.AttributeName) { attribute.AttributeValue = SelectedVal; break; } } } }
解决方法
您需要在显示组合框时找到它们,并在所选索引发生更改时附加一个事件处理程序(因为无法从列或单元本身获取该信息).
这意味着,遗憾的是,捕获事件CellEndEdit是没用的.
在下面的示例中,文本框中填充了所选的选项,但您可以执行任何其他操作,例如在枚举变量中选择特定值或其他任何值.
void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { if ( e.Control is ComboBox comboEdited ) { // Can also be set in the column,globally for all combo Boxes comboEdited.DataSource = ListBoxItems; comboEdited.AutoCompleteMode = AutoCompleteMode.Append; comboEdited.AutoCompleteSource = AutoCompleteSource.ListItems; // Attach event handler comboEdited.SelectedValueChanged += (sender,evt) => this.OnComboSelectedValueChanged( sender ); } return; } void OnComboSelectedValueChanged(object sender) { string selectedValue; ComboBox comboBox = (ComboBox) sender; int selectedindex = comboBox.Selectedindex; if ( selectedindex >= 0 ) { selectedValue = ListBoxItems[ selectedindex ]; } else { selectedValue = comboBox.Text; } this.Form.EdSelected.Text = selectedValue; }
找到complete source code for the table in which a column is a combobox in GitHub.
希望这可以帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。