none
DataGrid SelectionChanged Event to fill a TextBox with selected Row RRS feed

  • Question

  • Hello , in C# i can easily get data from a SQL Server database and put it in a datagrid with this code :

      private void GetPcListBtn_Click(object sender, RoutedEventArgs e)
            {
                string service = (string)ServiceCB.SelectedValue;
    
                comm = new SqlCommand("select T_COLLABORATEURS.CO_IDENT,T_PC.PC_ID,T_PC.PC_NOM,T_PC.PC_MODEL," +
                    "T_PC.PC_DATE_MES,T_PC.PC_COMM,T_PC.SERV_ID from T_COLLABORATEURS, T_PC" +
                    $" where T_COLLABORATEURS.CO_ID = T_PC.CO_ID and T_PC.SERV_ID = '{service}' ", conn);
                SqlDataAdapter dap = new SqlDataAdapter(comm);
                DataTable dt = new DataTable();
                dap.Fill(dt);
                PC_DT.ItemsSource = dt.DefaultView;
            }
            #endregion

    Then with the SelectionChanged event of the Datagrid control i can retrieve datas from a particular row and fill a Textbox with it with this code :

     private void PC_DT_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (PC_DT.SelectedItem is DataRowView oData)
                {
                    string pcNom = (string)oData["PC_NOM"];
                    string pcModel = (string)oData["PC_MODEL"];
                    RefPcTxtBox.Text = $"{pcNom} / {pcModel}";
                }
    
    
            }

    Now i want to do the same thing with linq to SQL.

    Get data from the database with this code :

     private void GetPcListBtn_Click(object sender, RoutedEventArgs e)
            {
                string service = (string)ServiceCB.SelectedValue;
                DataClasses1DataContext dc = new DataClasses1DataContext();
                var pcCo = from co in dc.T_COLLABORATEURS
                           join pc in dc.T_PC on co.CO_ID equals pc.CO_ID
                           where pc.SERV_ID == service
                           select new
                           {
                               pc.PC_ID,
                               co.CO_IDENT,
                               pc.PC_NOM,
                               pc.PC_MODEL,
                               pc.PC_DATE_MES,
                               pc.PC_COMM,
                               pc.SERV_ID
                           };
                PC_DT.ItemsSource = pcCo;
    
            }

    this work perfectly.

    But now i don't now how to write a code in the Datagrid SelectionChanged event to fill my Textbox with the selected row datas.

      private void PC_DT_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
    // ???
    
            }

    Thank you for helping me !


    JF Collombet ® CreateSpecificCulture

    samedi 9 mars 2019 06:49

Toutes les réponses

  • After searching on the web, i found this code :

     private void PC_DT_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
               
                var grid = (DataGrid)sender;
                string selection = "Selected Rows :";
                bool first = true;
                foreach(var row in grid.SelectedItems)
                {
                    if (!first) selection += "|";
                    first = false;
                    selection += row.ToString();
                }
                MessageBox.Show(selection, "Selected Rows");
            }

    But it returns the whole row datas. How can i get just the "PC_NOM"row data ?


    JF Collombet ® CreateSpecificCulture

    dimanche 10 mars 2019 08:08