How to select two items with the same name in different datagridview (wpf c#)

KwebenaAcquah-9104 306 Reputation points
2021-08-04T09:18:58.33+00:00

please; hello, i am having about 5 datagridview in my project with similar data column from sql database and with similar identity column so i would like to select every other row selected each time i select from any of the datagridview; so if a particular identity column row is selected i would like that specific row to be selected in all other datagrids in my application;

please this is what i have tried;

but its not working in wpf but works in windows form please some help me out; please

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int rowindex = dataGridView1.Rows[e.RowIndex].Index;
    int columnindex = dataGridView1.Columns[1].Index;

    foreach (DataGridViewRow row in dataGridView2.Rows)
        row.Selected = false;

    var cellValue1 = dataGridView1.Rows[rowindex].Cells[1].Value;  // 1 <= index of Name column in dgItems

    foreach (DataGridViewRow row in dataGridView2.Rows)
    {
        var cellValue2 = row.Cells[0].Value; // 0 <= index of Name column in dgItemList

        if (cellValue1 == cellValue2)
            row.Selected = true;
        else
            row.Selected = false;
    }
}

UPDATE;

Can Any One Help Me To Rewrite this Code For WPF this is what i want please

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,415 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-08-05T06:47:49.947+00:00

    Please try if this works for you:

            private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)  
            {  
                var re = e.OriginalSource as TextBlock;  
                var text=  re.Text;  
                foreach (DataRowView item in dataGrid2.ItemsSource)  
                {  
                    var cellValue1 = item.Row.ItemArray[1].ToString();  
                    if (cellValue1 == text)  
                        dataGrid2.SelectedItem = item;  
                     
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments