开发者

Copy Value to other cells

开发者 https://www.devze.com 2023-04-03 18:54 出处:网络
I am using a DGV that has many columns. Some of the columns will be blank and the user will need to fill in the columns manually.

I am using a DGV that has many columns. Some of the columns will be blank and the user will need to fill in the columns manually.

However, some of the columns that are blanks have similar values that need to be filled in.

For example, if I have this data below:

1 0201     0201R    8X2    1    1   FUJI    8MM
2 0402              8X2    1    1   FUJI    
3 0402              8X2    1    1   FUJI    
4 1650     1650C    8X2    1    1   FUJI    8MM
5 1650     1650C    8X2    1    1   FUJI    8MM

... So for row 2 and 3 there are 2 blank columns (columns 3 and 8).

So if the user enters in 0402A into row 2, column 3.. it will also be entered into row 3, column 3 since the beginning values of 0402 match. Also, the same thing will happen if the user enteres in the value 12MM into row 2, column 8 (it will be placed into row开发者_JS百科 3, column 8 if the line is matched)... Like below:

1 0201     0201R    8X2    1    1   FUJI    8MM
2 0402     0402A    8X2    1    1   FUJI    12MM     //notice that only '0402A' was entered one time.
3 0402     0402A    8X2    1    1   FUJI    12MM     //same with '12MM'
4 1650     1650C    8X2    1    1   FUJI    8MM
5 1650     1650C    8X2    1    1   FUJI    8MM

Better explanation: The 2nd column value will be the value that is compared to every entered value. Everytime a value is entered into a cell, it will copy that value to that same column in every other row that is equal to the original 2nd column value.

QUESTION

  • Is this possible to do? How can I do this?


EDIT: i have just understood that DGV == DataGridView... :) i have no experience with this, but you should do something similar to this...

private void dataGridView1_CellValueChanged( object sender, DataGridViewCellEventArgs e )
    {
        dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        string key = row.Cells[1].Value.ToString();

        foreach ( DataGridViewRow affected in dataGridView1.Rows)
        {
            if ( affected.Cells[1].Value.ToString( ) == key )
            {
                for ( int i=2; i < dataGridView1.Columns.Count; i++ )
                {
                    affected.Cells[i].Value = row.Cells[i].Value;
                }
            }
        }

        dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
    }


This is possible. After user entered any column, you program just go through each row of DGV, compare 2nd column with the editing row, and fill in the column accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消