[已解决]更改dataGridView中现有comboBox列的所选项目时如何进行更改 [英] [Solved] How to make a change when changing selected item of an existing comboBox column in a dataGridView
本文介绍了[已解决]更改dataGridView中现有comboBox列的所选项目时如何进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我遇到了一个问题 [ ^ ]更改dataGridView
中comboBox
列的选定项时如何更改textBox
单元格的内容.
Nuri先生给了我一个例子,但是我无法在我的代码中对其进行模拟,请问我想知道如何使用一个已经存在的comboBox
列而不创建一个新列.
这是努里先生给出的例子:
Hi all,
I was having a problem[^] how to change a content of textBox
cell when changing the selected Item of a comboBox
column in dataGridView
.
Mr. Nuri gives me an example but I am not able to simulate it in my code please I want to know how to do it using an already existing comboBox
column not creating a new one.
This is the example given by Mr. Nuri :
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ComboBox cbm;
DataGridViewCell currentCell;
// This DataTable will become a data source for the DataGridView
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create and fill a DataTable as a source of our combobox column
DataTable typedt = new DataTable();
typedt.Columns.Add("typeid", typeof(int));
typedt.Columns.Add("typename");
typedt.Columns.Add("typedescription");
typedt.Rows.Add(0, "Typed 0", "description0");
typedt.Rows.Add(1, "Typed 1", "description1");
typedt.Rows.Add(2, "Typed 2", "description2");
typedt.Rows.Add(3, "Typed 3", "description3");
typedt.Rows.Add(4, "Typed 4", "description4");
typedt.AcceptChanges();
// Fill the GridView source table
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("type", typeof(int));
dt.Columns.Add("typedescription");
for (int i = 0; i < 50; i++)
dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);
dt.AcceptChanges();
// Set the data source for the grid
dataGridView1.DataSource = dt;
// Create new combobox column and set it''s data source
DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
cbc.DataPropertyName = "type";
cbc.DisplayMember = "typename";
cbc.ValueMember = "typeid";
cbc.DataSource = typedt;
cbc.HeaderText = "Type";
// Replace the third column of the grid view with the combobox column
dataGridView1.Columns.RemoveAt(2);
dataGridView1.Columns.Insert(2, cbc);
// We will handle these events of the DataGridView
dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (cbm != null)
{
// Here we will remove the subscription for selected index changed
cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);
}
}
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// Here try to add subscription for selected index changed event
if (e.Control is ComboBox)
{
cbm = (ComboBox)e.Control;
if (cbm != null)
{
cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
}
currentCell = this.dataGridView1.CurrentCell;
}
}
void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
// Invoke method if the selection changed event occurs
BeginInvoke(new MethodInvoker(EndEdit));
}
void EndEdit()
{
// Change the content of appropriate cell when selected index changes
if (cbm != null)
{
DataRowView drv = cbm.SelectedItem as DataRowView;
if (drv != null)
{
this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();
this.dataGridView1.EndEdit();
}
}
}
}
}
预先感谢,
:)
更新:
由OP自己解决.请他在此处发布答案.
Thanks in advance,
:)
UPDATE:
Solved by OP himself. Asked him to post the answer here.
推荐答案
简单地,
在源文件和.cs文件中手动写入下拉selectindexchanged事件并设置autopostback = true.
然后在selectedindexchanged事件中,使用sender对象检索组合框selectedindex和text,并使用dropdownlist clientid获取所选行,如
DropDownList ddl =(DropDownList)sender;
字符串s = ddl.ClientID;//使用子字符串检索行索引
如果(ddl.SelectedItem.Text ==您选择的文本")
{
GridView1.Rows [0] .Cells [1] .Text =";
}
:cool:
simply,
manually write dropdown selectindexchanged event in sourcefile and .cs file and set autopostback=true.
then in selectedindexchanged event use sender object to retreive combobox selectedindex and text and use dropdownlist clientid to get selected row like
DropDownList ddl = (DropDownList)sender;
string s = ddl.ClientID;//use substring to retreive rowindex
if (ddl.SelectedItem.Text == "Your selected text")
{
GridView1.Rows[0].Cells[1].Text = "";
}
:cool:
这篇关于[已解决]更改dataGridView中现有comboBox列的所选项目时如何进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文