Hi! I am a beginer in C# and I have 2 forms in a project. The first one contains a dataGridview with 3 columns: nr,date and obs and a button. when i select a row and click on a button i want to copy the current nr,date and obs in some textboxes from the second form. how do i refer to thouse from the second form? Can anyone tell me, please? thanks.

Recommended Answers

All 10 Replies

Hi! I am a beginer in C# and I have 2 forms in a project. The first one contains a dataGridview with 3 columns: nr,date and obs and a button. when i select a row and click on a button i want to copy the current nr,date and obs in some textboxes from the second form. how do i refer to thouse from the second form? Can anyone tell me, please? thanks.

One way you could do it is to copy/paste from the Clipboard .

Another way is to directly pass that data to the second form.

What is your usage preference?

Do you have an instance of the second form open prior to the transfer or do you intend to create it on the push of the button.

One way you could do it is to copy/paste from the Clipboard .

Another way is to directly pass that data to the second form.

What is your usage preference?

I don't know what involves each one of this ways.. but i think the directly pass the data to the second form is better. can u tell me how to do that? thanks

Do you have an instance of the second form open prior to the transfer or do you intend to create it on the push of the button.

I want to create it when i click the button.

copy means,you wants to pass the values from one page to another right,try session or hiddenfield.

commented: Windows forms don't have sessions / hidden fields -1

copy means,you wants to pass the values from one page to another right,try session or hiddenfield.

Those are both web concepts and wont help in a winforms environment.

If you are creating an instance of the form2 when you click the button you can add the variables you want to pass to form2's constructor method:

//in form2
        private string _no, obs;
        private DateTime _date;

        private Form2(string no, DateTime date, string obs)
        {
            _no = no;
            _date = date;
            _obs = obs;
        }


//in form1
        private void button1_Click(object sender, EventArgs e)
        {
            string number = "123";
            DateTime date = DateTime.Now;
            string obs = "none";

            Form2 frm2 = new Form2(number, date, obs);
            frm2.Show();
        }

Remember to mark the thread as solved if this answered your question :)

Those are both web concepts and wont help in a winforms environment.

If you are creating an instance of the form2 when you click the button you can add the variables you want to pass to form2's constructor method:

//in form2
        private string _no, obs;
        private DateTime _date;

        private Form2(string no, DateTime date, string obs)
        {
            _no = no;
            _date = date;
            _obs = obs;
        }


//in form1
        private void button1_Click(object sender, EventArgs e)
        {
            string number = "123";
            DateTime date = DateTime.Now;
            string obs = "none";

            Form2 frm2 = new Form2(number, date, obs);
            frm2.Show();
        }

Remember to mark the thread as solved if this answered your question :)

maybe i'm missing something, but it doesn't work, too :(

TestData.cs

public class TestData
    {
        public string No { get; set; }

        public string OPS { get; set; }

        public DateTime Date { get; set; }
    }

Form1.cs

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

        private void Form1_Load(object sender, EventArgs e)
        {
            List<TestData> list = new List<TestData>();
            list.Add(new TestData() { No = "1", OPS = "test1", Date = DateTime.Now.AddDays(-1) });
            list.Add(new TestData() { No = "2", OPS = "test2", Date = DateTime.Now.AddDays(-2) });
            list.Add(new TestData() { No = "3", OPS = "test3", Date = DateTime.Now.AddDays(-3) });

            dataGridView1.DataSource = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            DataGridViewRow dgr = dataGridView1.SelectedRows[0];
            string no = dgr.Cells["No"].Value.ToString();
            string ops = dgr.Cells["OPS"].Value.ToString();
            DateTime date = (DateTime)dgr.Cells["Date"].Value;

            Form2 frm = new Form2(no, ops, date);
            frm.ShowDialog();
        }
    }

Form2.cs

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string no, string ops, DateTime date)
        {
            InitializeComponent();
            this.No = no;
            this.OPS = ops;
            this.Date = date;
        }

        public string No
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public string OPS
        {
            get { return textBox2.Text; }
            set { textBox2.Text = value; }
        }

        public DateTime Date
        {
            get { return dateTimePicker1.Value; }
            set { dateTimePicker1.Value = value; }
        }
    }

Main form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.editform
{
  public partial class frmMain : Form
  {
    private DataTable _dt;

    public frmMain()
    {
      InitializeComponent();
    }

    private static DataTable GetData()
    {
      DataTable result = new DataTable();
      //nr,date and obs 
      result.Columns.Add("nr", typeof(int));
      result.Columns.Add("date", typeof(DateTime));
      result.Columns.Add("obs", typeof(string));
      for (int i1 = 1; i1 <= 50; i1++)
      {
        DataRow row = result.NewRow();
        row["nr"] = i1;
        row["date"] = DateTime.Today.AddDays(i1);
        row["obs"] = Guid.NewGuid().ToString();
        result.Rows.Add(row);
      }
      return result;
    }

    private void RefreshData()
    {
      if (_dt != null)
      {
        _dt.Dispose();
      }
      _dt = GetData();
      dataGridView1.DataSource = _dt;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      RefreshData();
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
      using (frmEdit frm = new frmEdit())
      {
        StronglyTypedRow strongRow = frm.Edit(null);
        if (strongRow != null)
        {
          DataRow row = _dt.NewRow();
          row.BeginEdit();
          row["nr"] = strongRow.NR;
          row["date"] = strongRow.Date;
          row["obs"] = strongRow.OBS;
          row.EndEdit();
          _dt.Rows.Add(row);
          _dt.AcceptChanges();
        }
      }
    }

    private void buttonEdit_Click(object sender, EventArgs e)
    {
      if (dataGridView1.SelectedRows.Count != 1) return;

      DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
      StronglyTypedRow strongRow = new StronglyTypedRow(row);
      using (frmEdit frm = new frmEdit())
      {
        strongRow = frm.Edit(strongRow);
        if (strongRow != null)
        {
          row.BeginEdit();
          row["nr"] = strongRow.NR;
          row["date"] = strongRow.Date;
          row["obs"] = strongRow.OBS;
          row.EndEdit();
          _dt.AcceptChanges();
        }
      }
    }

    private void buttonDelete_Click(object sender, EventArgs e)
    {
      if (dataGridView1.SelectedRows.Count != 1) return;
      if (MessageBox.Show("Delete row", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
        row.Delete();
        _dt.AcceptChanges();
      }
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
      this.Close();
    }

  }
}

Strong row:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace daniweb.editform
{
  public class StronglyTypedRow
  {
    public int NR { get; set; }
    public DateTime Date { get; set; }
    public string OBS { get; set; }

    public StronglyTypedRow()
    {
    }
    public StronglyTypedRow(DataRow row)
      : this(Convert.ToInt32(row["nr"]), Convert.ToDateTime(row["date"]), Convert.ToString(row["obs"]))
    {
    }
    public StronglyTypedRow(int NR, DateTime Date, string OBS)
      :this()
    {
      this.NR = NR;
      this.Date = Date;
      this.OBS = OBS;
    }
  }
}

Edit form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.editform
{
  public partial class frmEdit : Form
  {
    private StronglyTypedRow result;

    public frmEdit()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Edit a row. Pass a null value to add a new row.
    /// </summary>
    /// <param name="row"></param>
    public StronglyTypedRow Edit(StronglyTypedRow row)
    {
      result = row;
      this.ShowDialog();
      return result;
    }

    private void frmEdit_Load(object sender, EventArgs e)
    {
      if (result != null)
      {
        textBoxNR.Text = result.NR.ToString("F0");
        textBoxOBS.Text = result.OBS;
        dateTimePicker1.Value = result.Date;
      }
      result = null;
    }


    private void buttonExit_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
      errorProvider1.Clear();
      int NR;
      if (!int.TryParse(textBoxNR.Text, out NR))
      {
        errorProvider1.SetError(textBoxNR, "Invalid integral value");
      }
      result = new StronglyTypedRow(NR, dateTimePicker1.Value, textBoxOBS.Text);
      this.Close();
    }

  }
}

Project is attached.

thanks a lot! it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.