Windows Apps中DataGridView的Combobox的selectedIndexChanged事件 [英] selectedIndexChanged event of Combobox of DataGridView in Windows Apps

查看:75
本文介绍了Windows Apps中DataGridView的Combobox的selectedIndexChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在DataGridView中跟踪Combobox的selectedIndexChanged事件

how to track selectedIndexChanged event of a Combobox in DataGridView

推荐答案

嗨!!! Meraj查看此链接

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/57df7b19-4929-47e3-9793-9a6fba278595/ [ ^ ]



http ://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4f95b09a-89bc-4bf7-aa2f-2e78ec4050e3/ [ ^ ]
Hi!!! Meraj check this link
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/57df7b19-4929-47e3-9793-9a6fba278595/[^]

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4f95b09a-89bc-4bf7-aa2f-2e78ec4050e3/[^]


ComboBoxCell不支持SelectedIndex属性,我找到了两种方法来实现。

1.设置ComboBoxCell的初始值。

例如,这是一个包含CountryID和CountryName的表

ComboBoxCell doesn''t support SelectedIndex property, I have found two way to achieve.
1. Set the initial value of the ComboBoxCell.
For example, here is a table with CountryID and CountryName
private DataTable dtCountry = new DataTable();
dtCountry.Columns.Add("CountryID", typeof(int));
dtCountry.Columns.Add("CountryName", typeof(string));
dtCountry.Rows.Add(1, "Canada");
dtCountry.Rows.Add(2, "USA");
dtCountry.Rows.Add(3, "Mexico");





我将创建一个新的DataGridViewComboBoxCell实例并使用dtCountry中的第一行设置其初始值。



I will create a new DataGridViewComboBoxCell instance and set its initial value with the first row in dtCountry.

DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell();
dgvCmbCell.DataSource = dtCountry;
dgvCmbCell.DisplayMember = "CountryName";
dgvCmbCell.ValueMember = "CountryID";
// use this cell on row[0] cell[1]
dataGridView1.Rows[0].Cells[1] = dgvCmbCell;
if (dataGridView1.Rows[0].Cells[1].Value.ToString() == "")
{
    dgvCmbCell.Value = dtCountry.Rows[0]["CountryID"];
}





2.单击单元格并想要编辑时,将SelectedIndex设置为0。所以你需要处理EditingControlShowing事件。



2. Set the SelectedIndex to 0 when you click the cell and want to edit it. So you need to handle EditingControlShowing event.

dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cmb = e.Control as ComboBox;
    if (cmb != null)
    {
       cmb.SelectedIndex = 0;
    }
}





请查看以下内容: - http://social.msdn.microsoft.com/Forums/en-US / winformsdesigner / thread / 57df7b19-4929-47e3-9793-9a6fba278595 / [^] [ ^ ]


这篇关于Windows Apps中DataGridView的Combobox的selectedIndexChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆