none
How to navigate into other form from current depending on some data

    Question

  • Hi,

    Sometimes <g class="gr_ gr_28 gr-alert gr_tiny gr_spell gr_run_anim ContextualSpelling multiReplace" data-gr-id="28" id="28">i</g> need to open another form according to some data from on current <g class="gr_ gr_91 gr-alert gr_gramm gr_run_anim Style multiReplace" data-gr-id="91" id="91">form ,</g> for <g class="gr_ gr_95 gr-alert gr_gramm gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="95" id="95">example</g> if <g class="gr_ gr_129 gr-alert gr_gramm gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="129" id="129">currently</g> <g class="gr_ gr_131 gr-alert gr_tiny gr_spell gr_run_anim ContextualSpelling multiReplace" data-gr-id="131" id="131">i</g> have a form with invoice data in <g class="gr_ gr_204 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="204" id="204">datagridview</g> and when <g class="gr_ gr_193 gr-alert gr_tiny gr_spell gr_run_anim ContextualSpelling multiReplace" data-gr-id="193" id="193">i</g> double click on some row how <g class="gr_ gr_260 gr-alert gr_tiny gr_spell gr_run_anim ContextualSpelling multiReplace" data-gr-id="260" id="260">i</g> can open other form contains the details of this invoice which selected on <g class="gr_ gr_330 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="330" id="330">datagridview</g>

    Thanks

    Sunday, April 9, 2017 9:01 AM

All replies

  • Hello,

    You need to read back your reply, can you read it as intended, do you really think we can understand it???


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 9, 2017 9:53 AM
    Moderator
  • I've stripped the markup from the original post as a public service:

    Sometimes i need to open another form according to some data from on current form , for example if currently i have a form with invoice data in datagridview and when i double click on some row how i can open other form contains the details of this invoice which selected on datagridview

    Sunday, April 9, 2017 1:05 PM
  • You can use the SelectedCells, SelectedRows, or SelectedColumns properties of a DataGridView.

    You have a variety of options for setting up a form so that it accepts some parameters, depending on what's most appropriate for you.  I model my thinking after the OpenFileDialog, which goes like this:

    // First you construct the new form.
    OpenFileDialog dlg = new OpenFileDialog();
    
    // Then you adjust properties of the form to set your
    // desired parameters
    dlg.InitialDirectory = "c:\\";
    dlg = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    dlg.FilterIndex = 2;
    dlg.RestoreDirectory = true;
    
    // Then you show the form, either directly with dlg.Show, or perhaps as a dialog like this:
    DialogResult result = dlg.ShowDialog();
    if( result == DialogResult.OK ) {
        // Then at a later time, you can check results the
        // same way:  By examining properties.
        // e.g.:
        string fileName = dlg.FileName;
        DoSomethingWithTheFileName( fileName );
    }
    

    Using this as a model, you should have properties of your Form2 class that allow you to set the parts when you show the form.  For example, if Form2 is a "details" form, and you want to be able to show the details for one record, you might have a property called "RecordId" that you will set when you show the form.

    for example:

        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private int recordId;
            public int RecordId
            {
                set
                {
                    recordId = value;
                    // Your code here to set up the form to show details for the record with id `value`.
                    // e.g.:
                    Thing thing = GetThingFromId( recordId );
                    labelName.Text = thing.Name;
                }
                get
                {
                    return recordId;
                }
            }
        }
    

    Sunday, April 9, 2017 1:35 PM
  • Okay, here is an example where the DataGridView is populated from a SQL-Server database table.

    NOTE: Ignore anything indicating CheckedListBox as I put the following code together from a code sample on CheckedListBox but the code below has no CheckedListBox.

    To be complete.

    Class to load data

    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace CheckListBoxFromSQL_Server
    {
        public class Operations
        {
            private string Server = "KARENS-PC";
            private string Catalog = "CheckedListBoxDatabase";
            private string ConnectionString = "";
            public Operations()
            {
                ConnectionString = $"Data Source={Server};Initial Catalog={Catalog};Integrated Security=True";
            }
            /// <summary>
            /// Get all records to show in the CheckedListBox
            /// </summary>
            /// <returns></returns>
            public DataTable GetAll()
            {
                var dt = new DataTable();
    
                using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString })
                {
                    using (SqlCommand cmd = new SqlCommand { Connection = cn })
                    {
    
                        cmd.CommandText = "SELECT id, Description, Quantity FROM Products)";
    
                        cn.Open();
                        dt.Load(cmd.ExecuteReader());
    
                    }
                }
    
                return dt;
    
            }
        }
    }

    Form code where I use a BindingSource that has a DataSource of type DataTable loaded from the class above. In turn the DataGridView.DataSource is set to the BindingSource.

    When the user double clicks a row I open a child form and pass the current row into the child form.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace CheckListBoxFromSQL_Server
    {
        public partial class MainForm : Form
        {
            private BindingSource bs;
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
                bs = new BindingSource();
                var ops = new Operations();
                bs.DataSource = ops.GetAll();
                dataGridView1.DataSource = bs;
            }
    
            private void dataGridView1_DoubleClick(object sender, EventArgs e)
            {
                DataRow row = ((DataRowView)bs.Current).Row;
                var f = new DetailsForm(row);
                try
                {
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        row.SetField<string>("Description",  f.Row.Field<string>("Description"));
                        row.SetField<int>("Quantity", f.Row.Field<int>("Quantity"));
                    }
                }
                finally
                {
                    f.Dispose();
                }
            }
        }
    }

    In the child form the row data is set to TextBox controls (granted the numeric field could be set to a NumericUpDown control or custom TextBox for numerics only). If the user presses Button1 we check if the data is valid in the controls. If data is valid the form is closed via DialogResult = DialogResult.OK.

    Note the logic in the detail form to change values in the passed DataRow then note the logic back in the main form to set the field data or not.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace CheckListBoxFromSQL_Server
    {
        public partial class DetailsForm : Form
        {
            public DataRow Row;
            public DetailsForm()
            {
                InitializeComponent();
            }
            public DetailsForm(DataRow row)
            {
                InitializeComponent();
                Row = row;
            }
    
            private void DetailsForm_Load(object sender, EventArgs e)
            {
                txtDescription.Text = Row.Field<string>("Description");
                txtQuantity.Text = Row.Field<int>("Quantity").ToString();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                int Quantity = 0;
                if (int.TryParse(txtQuantity.Text,out Quantity) && !string.IsNullOrWhiteSpace(txtDescription.Text))
                {
                    Row.SetField<string>("Description", txtDescription.Text);
                    Row.SetField<int>("Quantity", Quantity);
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Please enter valid information for each field");
                }
            }
        }
    }

    If you are not populating the DataGridView using a DataTable then this should had been mentioned up front. If the DataGridView is being populated by a TableAdapter the logic is the same only you need to cast items according to the strong typed classes in the Adapter(s). Same goes for using Entity Framework (I have a working example here).

    If you are not using the DataSource then consider it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, April 9, 2017 1:57 PM
    Moderator

  • I have written two blog posts about various ways to pass data between forms. There are numerous ways to do this type of thing. Take a look at Option 3 in my first post (it makes use of Interfaces) and the second post makes use of delegates/events:



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, April 9, 2017 4:36 PM
    Moderator