Обновление значения dataGridView из другой формы - PullRequest
0 голосов
/ 20 апреля 2020

Здравствуйте, эксперты, помогите, пожалуйста, у меня есть две формы в моем проекте, и у одной из этих форм1 есть dataGridView, и я хочу обновить gridview в соответствии с текстовым значением из формы 2,

я сделал обновление dgv, но все строка обновлена ​​по событию нажатия кнопки ex. у меня есть 4 строки с другим описанием и значением ячейки после события клика, все эти 4 строки обновляются до 1 значения.

Form 1
dgv cellmouseclick event
if (e.RowIndex >= 0)
            {
                DataGridViewRow row = dataGridView2.Rows[e.RowIndex];
                _choosenPart = row.Cells[0].Value.ToString();
                _choosenQty = row.Cells[2].Value.ToString();
                _choosenPrice = row.Cells[4].Value.ToString();
                _choosenAmount = row.Cells[5].Value.ToString();
                _choosenTotal = row.Cells[7].Value.ToString();
            }

            FrM_Edit EditGV = new FrM_Edit(this);
            EditGV.Show();



In form 2 

//constructor
Form F2_main = null;
public FrM_Edit(Form DgVForm1)
    {
     F2_main=DgVForm1;
     InitializeComponent();
    }

button click event
            r2main_choosenPart = textBox1.Text;
            r2main_choosenQty = textBox2.Text;
            r2main_choosenPrice = textBox3.Text;
            r2main_choosenAmount = textBox4.Text;
            r2main_choosenTotal = textBox5.Text;


            DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];            
            for (int i = 0; i < Main_dg.Rows.Count; i++)
            {
            Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
            Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
            Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
            Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
            Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;            
            }
            this.Close();

1 Ответ

0 голосов
/ 20 апреля 2020

// это для l oop принимает все строки вашего DataGridView. И это нормально, что вы видите, что все строки изменяются, потому что для всех строк вы применяете один и тот же код.

        DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];            
        for (int i = 0; i < Main_dg.Rows.Count; i++)
        {
          Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
          Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
          Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
          Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
          Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;            
        }

Вместо этого вам следует обновить только строку, которую вы хотите обновить. Вы можете использовать разные методы. Например:

        for (int i = 0; i < Main_dg.Rows.Count; i++)
        {
          if( Main_dg.Rows[i].Cells[0].Value == "valueToChange") //Checking an ID or other value.
           {
            Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
            Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
            Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
            Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
            Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal; 
            break; // To break after finding the row that you are looking for.           
           }
        }

Или

        for (int i = 0; i < Main_dg.Rows.Count; i++)
        {
          if(i == 2) //to update the third row of the grid.
           {
            Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
            Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
            Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
            Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
            Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;   
            break; // To break after finding the row that you are looking for.    
           }
        }

Или вы можете использовать выбранную строку DataGridViews для обновления.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...