Understanding Datagridview Data Retrieval

Asked 2 years ago, Updated 2 years ago, 34 views

Show Datagridview in Form 1 and
Press the Search menu in Form 1 and click
The Form 2 dialog opens and enters the name you want to search for.
Press Search for Form 2 to focus on the appropriate name for Form 1.

I would like to create a program called
Datagridview data for Form 1 between Form 1 and Form 2 or
I can't pass the textbox data in Form 2.

Below is the program in the middle.

Form 1

namespace datagrid
{
    public partial class Form 1:Form
    {
        public Form 1()
        {
            InitializeComponent();

            DataSetds=new DataSet();
            DataTablet = new DataTable();

            // Define three columns.
            dt.Columns.Add("PNName", Type.GetType("System.String"));
            dt.Columns.Add("Age", Type.GetType("System.Int32"));
            // dt.Columns.Add("C", Type.GetType("System.DateTime"));

            // Add 4 lines.
            for (inti=0; i<4;i++)
            {
                DataRow = dt.NewRow();
                switch(i){
                    case0:
                        row["PName"] = "Taro";
                        row["Age"] = 20;
                        break;
                    case1:
                        row["PName"] = "Jiro";
                        row["Age"] = 21;
                        break;
                    case2:
                        row["PName"] = "Saburo";
                        row["Age"] = 22;
                        break;
                    case3:
                        row["PName"] = "Shiro";
                        row["Age"] = 23;
                        break;
                }
                dt.Rows.Add(row);
            }

            // Add dt to DataSet.
            ds.Tables.Add(dt);

            dt.TableName="Table1";
            // Displayed in datagridview
            dataGridView 1.DataSource=dt;
        }

        private void search ToolStripMenuItem_Click (object sender, EventArgse)
        {
            Form 2 FS = new Form 2();
            if (FS.ShowDialog() == DialogResult.OK)
            {

            }

        }
    }
}

Form 2

namespace datagrid
{
    public partial class Form 2:Form
    {
        public Form 2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgse)
        {
            string searchName = textBox1.Text;
            // Search for the same string as serchName in the PName column
            for (inti=0;; i++)
            {

            }
        }
    }
}

This is the Form 1 and Form 2 screens.I look forward to your kind cooperation.
Enter a description of the image here

c#

2022-09-30 16:50

1 Answers

Define the properties to publish textbox1.Text in Form2 and refer to the Search ToolStripMenuItem_Click as the normal design.

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

    public string SearchName
    {
        get
        {
            return textBox1.Text;
        }
        set
        {
            textBox1.Text=value;
        }
    }
    // Button1_Click is not required if DialogResult property is specified
}

Form 1

private void Search ToolStripMenuItem_Click (object sender, EventArgse)
{
    Form 2 FS = new Form 2();
    if (FS.ShowDialog() == DialogResult.OK)
    {
        // Process in FS.SearchName.
    }
}

An example of the search process is as follows:

 for (vari=0;i<dataGridView1.RowCount;i++)
{
    var=dataGridView1.Rows[i];
    if(object.Equals(((DataRowView)r.DataBoundItem)["PName", FS.SearchName))
    {
        dataGridView1.ClearSelection();
        r.Selected = true;
        break;
    }
}


2022-09-30 16:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.