Error passing information from a datagrid line of a form to the datagrid of another form

Asked

Viewed 167 times

0

I have a following error when passing information of the selected line from a Form1 grid to the form2 grid

Follow the code of the Form1 grid

 private void button1_Click(object sender, EventArgs e)
    {

        for (Int32 index = 0; index < dgw_separacaopic.Rows.Count; index++)
        {
            if (bool.Parse(dgw_separacaopic.Rows[index].Cells[0].FormattedValue.ToString()) == true)
            {
                frmSepfatpic sep = new frmSepfatpic();
                sep.Pedido = dgw_separacaopic.CurrentRow.Cells[4].Value.ToString();
                sep.Show();

            }

        }

Follow the form2 grid code that is receiving the value

 public partial class frmSepfatpic : Form
{
    public string Pedido { get; set; }
    //public string Item { get; set; }
    //public string Cliente { get; set; }
    //public string QTDA { get; set; }
    public frmSepfatpic()
    {
        InitializeComponent();
    }

    private void frmSepfatpic_Load(object sender, EventArgs e)
    {
        dgw_sepfatpic.CurrentRow.Cells[0].Value = Pedido;
    }
}

Error image in form2 inserir a descrição da imagem aqui

1 answer

1


This happens because you are trying to access a line that does not yet exist, try to do this way:

 dataGridView1.Columns.Add("collumName", "HeaderText");
 dataGridView1.Columns.Add("collumName1", "HeaderText1");

 string[] row1 = new string[] { "column2 value", "column6 value" };
 dataGridView1.Rows.Add(row1);
  • 1

    Lodi thanks for the help, but now this presenting an error that the column index can not be negative..

  • 1

    Additional information: The index was out of range. It should be non-negative and smaller than the collection size.In this line dgw_sepfatpic.Columns[1]. Name = "REQUEST";

  • 1

    "The index was out of range. It must be non-negative and smaller than the collection size. r nName of parameter: index"}

  • I modified the answer, you must add the column before adding the new row. make a test, if it works do not forget to mark as reply and give an upvote...

  • Perfect Lodi thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.