Home > Software design >  Pass the third value from Datagridview to textbox, after pass two value to Select row
Pass the third value from Datagridview to textbox, after pass two value to Select row

Time:07-13

How can I display the value of the third column of the Datagridview in Textbox, pass to the value of the first and second columns from Textboxes

Name City Age
Jack LA 38
Mark NY 25
Vicky NY 31

when I pass the jack and LA select the row and show the 38 in Textbox

CodePudding user response:

I reproduce your problem.

UI page:

enter image description here

After clicking the button to add data, if only one value is entered in the text box, the age cannot be queried.

enter image description here

Enter the values of the two text boxes and click the show button to successfully query the age.

enter image description here

The specific logic is: click the add and button2 buttons to fill in the data, and then enter the value of the text box and click the show button to query the age.

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

    private void button1_Click(object sender, EventArgs e)
    {
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[0].Cells[0].Value = "jack";
        this.dataGridView1.Rows[0].Cells[1].Value = "LA";
        this.dataGridView1.Rows[0].Cells[2].Value = "38";
        this.dataGridView1.Rows[1].Cells[0].Value = "Mark";
        this.dataGridView1.Rows[1].Cells[1].Value = "NY";
        this.dataGridView1.Rows[1].Cells[2].Value = "25";
     
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index].Cells[0].Value = "Vicky";
        this.dataGridView1.Rows[index].Cells[1].Value = "NY";
        this.dataGridView1.Rows[index].Cells[2].Value = "31";
    }

    private void button3_Click(object sender, EventArgs e)
    {
       
        if (this.textBox2.Text.Equals("") || this.textBox3.Text.Equals(""))
        {
            return;
        }
        // Linq fuzzy query
        IEnumerable<DataGridViewRow> enumerableList = this.dataGridView1.Rows.Cast<DataGridViewRow>();
        List<DataGridViewRow> list = (from item in enumerableList
                                      where item.Cells[0].Value.ToString().IndexOf(this.textBox2.Text) >= 0
                                      select item).ToList();

       
        List<DataGridViewRow> list1 = (from item in enumerableList
                                      where item.Cells[1].Value.ToString().IndexOf(this.textBox3.Text) >= 0
                                      select item).ToList();
       
        if (list.Count > 0 && list1.Count > 0 )
        {
           
            int matchedRowIndex = list[0].Index;
            
            textBox1.Text = this.dataGridView1.Rows[matchedRowIndex].Cells[2].Value.ToString();
        }


    }
}

Hope it helps you.

CodePudding user response:

It is like the Search, but I need to pass the value not select the row to get result

Like This

1-you pass the two value from textboxs 2- Select the row when found, it Automatically 3- show the result into textbox

  • Related