1

2行目のアイテムを選択すると、1行目が変更され、1行目で選択されたアイテムが変更され、2行目で選択したものがコピーされます。

これらの変更を排除する方法をお聞きしたいのですが。

アイテムをクリアしたためかどうかはわかりません。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);
        }
    }
}
4

1 に答える 1

2

ここでの問題は、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

これで問題が解決することを願っています。

PSステートメントe.RowIndex == indexでの使用に戻る必要があります。if

于 2013-02-03T13:30:23.730 に答える