主要作法:
在datagridview中设置了三个下拉框,是在编辑列中设为DataGridViewComboBoxColumn的办法,然后作成联动的。主要方法就是private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//选择类别
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 1 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox1 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox1.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
}
//选择名称
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 3 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox2 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox2.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittet);
}
//选择规格
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 4 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox3 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox3.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittes);
}
} private void dataGridViewComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
//将相关的值清空
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = "";
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].Value = "";
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = "";
//处理业务
DataGridViewComboBoxCell titleCombox = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["item_name"] as DataGridViewComboBoxCell;
titleCombox.Items.Clear();
OleDbDataReader readTitle = rkInsert.getread("select item_name from item_table where item_type_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
while (readTitle.Read())
titleCombox.Items.Add(readTitle["item_name"].ToString());
titleCombox.Value = 1;
titleCombox.Dispose();
} private void dataGridViewComboBox_SelectionChangeCommittet(object sender, EventArgs e)
{
//处理业务
DataGridViewComboBoxCell guigiCombox = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["content"] as DataGridViewComboBoxCell;
guigiCombox.Items.Clear(); OleDbDataReader readGuige = rkInsert.getread("select item_type from item_table where item_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
while (readGuige.Read())
guigiCombox.Items.Add(readGuige["item_type"].ToString());
guigiCombox.Value = 1;
guigiCombox.Dispose();
OleDbDataReader readCode = rkInsert.getread("select item_code,item_type,item_unit from item_table where item_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
readCode.Read();
if (readCode.HasRows)
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = readCode["item_code"].ToString();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = readCode["item_unit"].ToString();
}
readCode.Close();
} private void dataGridViewComboBox_SelectionChangeCommittes(object sender, EventArgs e)
{
OleDbDataReader readGuige = rkInsert.getread("select item_code,item_unit from item_table where item_type='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
readGuige.Read();
if (readGuige.HasRows)
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = readGuige["item_code"].ToString();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = readGuige["item_unit"].ToString();
}
readGuige.Close();
} private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridViewComboBox1 != null)
{
dataGridViewComboBox1.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
this.dataGridViewComboBox1 = null;
}
if (this.dataGridViewComboBox2 != null)
{
dataGridViewComboBox2.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittet);
this.dataGridViewComboBox2 = null;
}
if (this.dataGridViewComboBox3 != null)
{
dataGridViewComboBox3.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittes);
this.dataGridViewComboBox3 = null;
}
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[8].Value = Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[6].Value) * Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[7].Value);
sumUpdate();
}
。
办法是从网上找的,现在问题是,如果一直编辑没有任何问题,但一旦中途把焦点移开datagridview,比如说要修改一下datagridview之外的东西的话,那么重新点击下拉框就会出错,出错方式是这样的,没有提示,只是在点击下拉时下拉框狂闪,而且无法把选中的项目显示在下拉框上,不过实际以经选中了,只是不显示而已,因为我通过联动发现二级的下拉框已经有变化了。不知为什么。
在datagridview中设置了三个下拉框,是在编辑列中设为DataGridViewComboBoxColumn的办法,然后作成联动的。主要方法就是private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//选择类别
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 1 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox1 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox1.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
}
//选择名称
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 3 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox2 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox2.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittet);
}
//选择规格
if (e.Control is DataGridViewComboBoxEditingControl && this.dataGridView1.CurrentCell.ColumnIndex == 4 && this.dataGridView1.CurrentCell.RowIndex != -1)
{
this.dataGridViewComboBox3 = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox3.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittes);
}
} private void dataGridViewComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
//将相关的值清空
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = "";
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].Value = "";
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = "";
//处理业务
DataGridViewComboBoxCell titleCombox = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["item_name"] as DataGridViewComboBoxCell;
titleCombox.Items.Clear();
OleDbDataReader readTitle = rkInsert.getread("select item_name from item_table where item_type_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
while (readTitle.Read())
titleCombox.Items.Add(readTitle["item_name"].ToString());
titleCombox.Value = 1;
titleCombox.Dispose();
} private void dataGridViewComboBox_SelectionChangeCommittet(object sender, EventArgs e)
{
//处理业务
DataGridViewComboBoxCell guigiCombox = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["content"] as DataGridViewComboBoxCell;
guigiCombox.Items.Clear(); OleDbDataReader readGuige = rkInsert.getread("select item_type from item_table where item_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
while (readGuige.Read())
guigiCombox.Items.Add(readGuige["item_type"].ToString());
guigiCombox.Value = 1;
guigiCombox.Dispose();
OleDbDataReader readCode = rkInsert.getread("select item_code,item_type,item_unit from item_table where item_name='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
readCode.Read();
if (readCode.HasRows)
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = readCode["item_code"].ToString();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = readCode["item_unit"].ToString();
}
readCode.Close();
} private void dataGridViewComboBox_SelectionChangeCommittes(object sender, EventArgs e)
{
OleDbDataReader readGuige = rkInsert.getread("select item_code,item_unit from item_table where item_type='" + ((ComboBox)sender).Text + "'");
//Read()方法用来读取SqlDataReader对象中的记录
readGuige.Read();
if (readGuige.HasRows)
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value = readGuige["item_code"].ToString();
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value = readGuige["item_unit"].ToString();
}
readGuige.Close();
} private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridViewComboBox1 != null)
{
dataGridViewComboBox1.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
this.dataGridViewComboBox1 = null;
}
if (this.dataGridViewComboBox2 != null)
{
dataGridViewComboBox2.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittet);
this.dataGridViewComboBox2 = null;
}
if (this.dataGridViewComboBox3 != null)
{
dataGridViewComboBox3.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommittes);
this.dataGridViewComboBox3 = null;
}
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[8].Value = Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[6].Value) * Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[7].Value);
sumUpdate();
}
。
办法是从网上找的,现在问题是,如果一直编辑没有任何问题,但一旦中途把焦点移开datagridview,比如说要修改一下datagridview之外的东西的话,那么重新点击下拉框就会出错,出错方式是这样的,没有提示,只是在点击下拉时下拉框狂闪,而且无法把选中的项目显示在下拉框上,不过实际以经选中了,只是不显示而已,因为我通过联动发现二级的下拉框已经有变化了。不知为什么。
看一下事件有没有问题。如果狂闪可能进入类似死循环状态了。。一点事例:
#region 显示为下拉框
private void dgMain_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
ComboBox cb = (ComboBox)e.Control;
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}
catch
{
return;
}
}
#endregion #region 定义下拉框委托
private void cb_SelectedIndexChanged(object sender,EventArgs e)
{
ComboBox combo = (ComboBox)sender;
if (combo.SelectedValue != null)
{
DataRowView drv = (DataRowView)combo.Items[combo.SelectedIndex];
dgMain.Rows[dgMain.CurrentRow.Index].Cells["CitationTableChineseName"].Value = drv[2].ToString();
}
}
#endregion