消除更改
本文关键字: | 更新日期: 2023-09-27 17:57:07
当我选择第二行中的项目时,第一行会更改,第一行中的选定项目会更改并复制我在第二行中选择的内容
我只想问,如何消除这些变化?
我不知道是不是因为清除了ComboBox
中的项目,如果我删除了column.Items.Clear()
它运行良好,但我还需要清除ComboBox
中的项目
public partial class MultipleEntry : Form
{
DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn col2 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn col3 = new DataGridViewComboBoxColumn();
DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn col5 = new DataGridViewComboBoxColumn();
DataGridViewTextBoxColumn col6 = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn col7 = new DataGridViewComboBoxColumn();
DataGridViewTextBoxColumn col8 = new DataGridViewTextBoxColumn();
public MultipleEntry()
{
InitializeComponent();
}
private void UNA()
{
string strQuery = "Select TypeName from TYPE where delType='False'";
OleDbDataAdapter adap = new OleDbDataAdapter(strQuery, Program.objConn);
OleDbCommandBuilder build = new OleDbCommandBuilder(adap);
DataTable dt = new DataTable();
adap.Fill(dt);
BindingSource bind = new BindingSource();
bind.DataSource = dt;
//Type
col1.DataPropertyName = "TypeName";
col1.HeaderText = "Type";
col1.Width = 100;
col1.DataSource = bind;
col1.ValueMember = "TypeName";
col1.DisplayMember = "TypeName";
grdMultiple.Columns.Add(col1);
//Category
col2.DataPropertyName = "CategoryName";
col2.HeaderText = "Category";
col2.Width = 100;
grdMultiple.Columns.Add(col2);
//Product Description
col3.DataPropertyName = "ProductDetails";
col3.HeaderText = "Product";
col3.Width = 150;
grdMultiple.Columns.Add(col3);
//Unit
col5.DataPropertyName = "UnitName";
col5.HeaderText = "Unit";
col5.Width = 75;
grdMultiple.Columns.Add(col5);
//Supplier
col7.HeaderText = "Supplier";
col7.Width = 150;
grdMultiple.Columns.Add(col7);
//Quantity
col4.HeaderText = "Qty";
col4.Width = 75;
grdMultiple.Columns.Add(col4);
//Price
col8.HeaderText = "Price";
col8.Width = 75;
grdMultiple.Columns.Add(col8);
//Serial Number
col6.HeaderText = "Serial No";
col6.Width = 80;
grdMultiple.Columns.Add(col6);
}
private void MultipleEntry_Load(object sender, EventArgs e)
{
//try
//{
Program.objConn = new OleDbConnection(Program.Connection);
Program.objConn.Open();
UNA();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
string value;
private void grdMultiple_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
int index = grdMultiple.CurrentCell.RowIndex;
if(e.ColumnIndex==0)
{
MessageBox.Show(e.ColumnIndex.ToString() + e.RowIndex.ToString());
object newValue = grdMultiple.CurrentRow.Cells[0].Value;
value = newValue.ToString();
MessageBox.Show(value);
string one = "select c.CategoryName,t.TypeName from CATEGORY c inner join PRODUCT p on p.CategoryNo = c.CategoryNo inner join TYPE t on t.TypeNo=p.TypeNo where t.TypeName='" + value + "'";
OleDbCommand cmdone = new OleDbCommand(one, Program.objConn);
OleDbDataReader rdrOne = cmdone.ExecuteReader();
col2.Items.Clear();
while (rdrOne.Read())
{
col2.Items.Add(rdrOne[0].ToString());
}
}
else if (e.ColumnIndex == 1 && e.RowIndex >=1)
{
MessageBox.Show(e.ColumnIndex.ToString() + e.RowIndex.ToString());
string one = "select c.CategoryName,t.TypeName from CATEGORY c inner join PRODUCT p on p.CategoryNo = c.CategoryNo inner join TYPE t on t.TypeNo=p.TypeNo where t.TypeName='" + value + "'";
OleDbCommand cmdone = new OleDbCommand(one, Program.objConn);
OleDbDataReader rdrOne = cmdone.ExecuteReader();
while (rdrOne.Read())
{
col2.Items.Add(rdrOne[0].ToString());
}
}
else if (e.ColumnIndex == 2 && e.RowIndex >= 1)
{
MessageBox.Show(e.ColumnIndex.ToString() + e.RowIndex.ToString());
col3.Items.Clear();
object newTwo = grdMultiple.CurrentRow.Cells[1].Value;
//MessageBox.Show(newTwo.ToString());
string two = "select p.ProductDetails,c.CategoryName from PRODUCT p inner join Category c on c.CategoryNo = p.CategoryNo where c.CategoryName='" + newTwo.ToString() + "'";
OleDbCommand cmdtwo = new OleDbCommand(two, Program.objConn);
OleDbDataReader rdrtwo = cmdtwo.ExecuteReader();
while (rdrtwo.Read())
{
col3.Items.Add(rdrtwo[0].ToString());
}
}
else if (e.ColumnIndex == 3 && e.RowIndex >= 1)
{
MessageBox.Show(e.ColumnIndex.ToString() + e.RowIndex.ToString());
col5.Items.Clear();
string three = "select u.UnitName from UNIT u";
OleDbCommand cmdThree = new OleDbCommand(three, Program.objConn);
OleDbDataReader rdrThree = cmdThree.ExecuteReader();
while (rdrThree.Read())
{
col5.Items.Add(rdrThree[0].ToString());
}
}
else if (e.ColumnIndex == 4 && e.RowIndex >= 1)
{
MessageBox.Show(e.ColumnIndex.ToString() + e.RowIndex.ToString());
col7.Items.Clear();
string four = "select SupplierCompany from SUPPLIER";
OleDbCommand cmdFour = new OleDbCommand(four, Program.objConn);
OleDbDataReader rdrFour = cmdFour.ExecuteReader();
while (rdrFour.Read())
{
col7.Items.Add(rdrFour[0].ToString());
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
这里的问题是您不正确地使用DataGridViewComboBoxColumn.Items
属性。此属性用于访问该列中所有单元格的值集合,因此每当调用 col2.Items.Clear()
时,都会清除整列中的组合框。要单独访问单元格的值集合,您应该执行以下操作:
//get the cell in selected row in 2nd column
var cell2 = (DataGridViewComboBoxCell)grdMultiple.Rows[index].Cells[2];
//clear the combo box value collection only for this cell
cell2.Items.Clear()
//do the rest accessing the cell's value collection like in the previous line
希望这能解决您的问题。
附言您应该返回到在if
语句中使用e.RowIndex == index
。