Listview c# passing value to the next form

Asked

Viewed 504 times

0

I rode a form in c# and this form is responsible for showing all data from my table on Bd and put this data into a listview, can thus select an item from the listview and rode another form where he will receive the ID of the field that was selected in the listview.

That’s when it started to complicate how I can pass on that value to my other form, because he does not recognize the command Listview.selectedvalue, for me to store the id of the selected line and pass as a search parameter in the other form

  • Is a listview or datagridview?

1 answer

0


Via construtor of classe who receives the ID of ListView. In the code below in evento ListView1_MouseDoubleClick would be a clear example of sending the ID via construtor.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.Columns.Add("Codigo", "Código");
            listView1.Columns.Add("Nome", "Nome");

            ListViewItem item0001 = listView1.Items.Add("0001");
            item0001.SubItems.Add("Teste 1");

            ListViewItem item0002 = listView1.Items.Add("0002");
            item0002.SubItems.Add("Teste 2");

            listView1.FullRowSelect = true;

            listView1.MouseDoubleClick += ListView1_MouseDoubleClick;

        }

        private void ListView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListView listView = ((ListView)sender);
            if (listView != null)
            {
                if (e.X >= 0 && e.Y >= 0)
                {
                    ListViewItem listViewItem = listView.GetItemAt(e.X, e.Y);
                    if (listViewItem != null)
                    {
                        int id;
                        if (int.TryParse(listViewItem.Text, out id))
                        {
                            Form2 frm = new Form2(id);
                            frm.ShowDialog();
                        }
                    }
                }
            }            
        }
    }
}

In the form2 create a paramento in the construtor that will receive the amount sent from form1:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private int _id;
        public Form2(int Id)
        {
            InitializeComponent();
            _id = Id;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //code
            //_id enviado do form1
            label1.Text = $"{_id}";
        }
    }
}

with the _id has the option to load data, manipulate, etc.

  • right... I just don’t understand what you’re calling e.X and.Y where are these values?? Because from what I understand, Voce tells to take these values as parameters, and only after that sends the id to the other form.

  • the X and Y are from the parish MouseEventArgs e will indicate the position at the time of the click. These values indicate which row is selected and with the row selected, I take the code I simulated in the first column and send it to the form2 via builder

Browser other questions tagged

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