none
Display selected gridview row on text boxes in C#

    Question

  • How can I display the row I select in my  RAD grid view and display it in my Textboxes ......

     

    this is how I display my rows in my telerik rad gridview

     

     
              oRow.Item("Del_Number") = txtDel_Number.Text
                oRow.Item("Del_Address1") = txtDel_Address1.Text
                oRow.Item("Del_Address2") = txtDel_Address2.Text
                oRow.Item("Del_Address3") = txtDel_Address3.Text
                oRow.Item("Del_Address4") = txtDel_Address4.Text
                oRow.Item("Del_Address5") = txtDel_Address5.Text
                oRow.Item("Client_Name") = CustomerNameTextBox.Text
                oRow.Item("Dispatch_Instructions") = txtDispatch_Instructions.Text
                oRow.Item("Operations_Instructions") = txtOperations_Instructions.Text
                oRow.Item("Type") = txtType.Text
                tblDeliveries.Rows.Add(oRow)
    

    • Moved by CoolDadTxMVP Tuesday, August 20, 2013 1:58 PM Winforms related
    Tuesday, August 20, 2013 11:52 AM

Answers

  • Assuming the name of your DataGridView is dataGridView1 and that you will select one Row or if multiple rows are selected, we only take the first one, here is your code sir:

    txtDel_Number.Text = dataGridView1.SelectedRows[0].Cells["Del_Number"].Value.ToString();               
    txtDel_Address1.Text   =    dataGridView1.SelectedRows[0].Cells["Del_Address1"].Value.ToString();             
    txtDel_Address2.Text   =   dataGridView1.SelectedRows[0].Cells["Del_Address2"].Value.ToString();              
    txtDel_Address3.Text  =    dataGridView1.SelectedRows[0].Cells["Del_Address3"].Value.ToString();              
    txtDel_Address4.Text   =   dataGridView1.SelectedRows[0].Cells["Del_Address4"].Value.ToString();              
    txtDel_Address5.Text    =   dataGridView1.SelectedRows[0].Cells["Del_Address5"].Value.ToString();              
    CustomerNameTextBox.Text  =     dataGridView1.SelectedRows[0].Cells["Client_Name"].Value.ToString();               
    txtDispatch_Instructions.Text =      dataGridView1.SelectedRows[0].Cells["Dispatch_Instructions"].Value.ToString();   
    txtOperations_Instructions.Text  =     dataGridView1.SelectedRows[0].Cells["Operations_Instructions"].Value.ToString();
    txtType.Text   =    dataGridView1.SelectedRows[0].Cells["Type"].Value.ToString();                   
    

    • Marked as answer by Hefff Tuesday, August 20, 2013 2:05 PM
    • Unmarked as answer by Hefff Tuesday, August 20, 2013 2:21 PM
    • Marked as answer by Hefff Wednesday, August 21, 2013 1:53 PM
    Tuesday, August 20, 2013 1:32 PM
  • hi heff ,

    to show selected row value in textbox you need to program on grid view SelectedIndexChanged event .... then find the selected row fetch the value and then show it .. find the code below

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
         //Grab the selected row
         GridViewRow row = GridView1.SelectedRow;
    
         //Find your textbox within the row
         TextBox yourTextbox = row.FindControl("TextBox1") as TextBox;
    
         //Check if your textbox was found
         if(yourTextbox != null)
         {
               //Get your value here
               var yourValue = yourTextbox.Text;
         }
    }


    • Proposed as answer by Neha Rai_MCT Tuesday, August 20, 2013 1:25 PM
    • Marked as answer by Hefff Tuesday, August 20, 2013 2:05 PM
    Tuesday, August 20, 2013 1:24 PM

All replies

  • hi heff ,

    to show selected row value in textbox you need to program on grid view SelectedIndexChanged event .... then find the selected row fetch the value and then show it .. find the code below

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
         //Grab the selected row
         GridViewRow row = GridView1.SelectedRow;
    
         //Find your textbox within the row
         TextBox yourTextbox = row.FindControl("TextBox1") as TextBox;
    
         //Check if your textbox was found
         if(yourTextbox != null)
         {
               //Get your value here
               var yourValue = yourTextbox.Text;
         }
    }


    • Proposed as answer by Neha Rai_MCT Tuesday, August 20, 2013 1:25 PM
    • Marked as answer by Hefff Tuesday, August 20, 2013 2:05 PM
    Tuesday, August 20, 2013 1:24 PM
  • Assuming the name of your DataGridView is dataGridView1 and that you will select one Row or if multiple rows are selected, we only take the first one, here is your code sir:

    txtDel_Number.Text = dataGridView1.SelectedRows[0].Cells["Del_Number"].Value.ToString();               
    txtDel_Address1.Text   =    dataGridView1.SelectedRows[0].Cells["Del_Address1"].Value.ToString();             
    txtDel_Address2.Text   =   dataGridView1.SelectedRows[0].Cells["Del_Address2"].Value.ToString();              
    txtDel_Address3.Text  =    dataGridView1.SelectedRows[0].Cells["Del_Address3"].Value.ToString();              
    txtDel_Address4.Text   =   dataGridView1.SelectedRows[0].Cells["Del_Address4"].Value.ToString();              
    txtDel_Address5.Text    =   dataGridView1.SelectedRows[0].Cells["Del_Address5"].Value.ToString();              
    CustomerNameTextBox.Text  =     dataGridView1.SelectedRows[0].Cells["Client_Name"].Value.ToString();               
    txtDispatch_Instructions.Text =      dataGridView1.SelectedRows[0].Cells["Dispatch_Instructions"].Value.ToString();   
    txtOperations_Instructions.Text  =     dataGridView1.SelectedRows[0].Cells["Operations_Instructions"].Value.ToString();
    txtType.Text   =    dataGridView1.SelectedRows[0].Cells["Type"].Value.ToString();                   
    

    • Marked as answer by Hefff Tuesday, August 20, 2013 2:05 PM
    • Unmarked as answer by Hefff Tuesday, August 20, 2013 2:21 PM
    • Marked as answer by Hefff Wednesday, August 21, 2013 1:53 PM
    Tuesday, August 20, 2013 1:32 PM
  • you cannot convert grid view Value to string but I can Understand your approach.....

    Tuesday, August 20, 2013 2:23 PM
  • you cannot convert grid view Value to string but I can Understand your approach.....

    Really???????

    If we are talking about Windows Forms in C#,

    Yes you can sir!

    Tuesday, August 20, 2013 2:36 PM
  • I'm Kinda converting a C# app to VB.net 

    and im not using ur normal Datagrid windows controls  I'm using Telerik Rad Controls

    so these may affect the norms il post my code when done......

     

    Tuesday, August 20, 2013 2:41 PM
  • For Each oRow As DataRow In tblDeliveries.Rows
                Dim tmpTable As New Dispatch_Template
                tmpTable.Del_Number = oRow.Item("Del_Number").ToString()
                tmpTable.Client_Name = oRow.Item("Client_Name").ToString()
                tmpTable.Application = oRow.Item("Application").ToString()
                tmpTable.Del_Address1 = oRow.Item("Del_Address1").ToString()
                tmpTable.Del_Address2 = oRow.Item("Del_Address2").ToString()
                tmpTable.Del_Address3 = oRow.Item("Del_Address3").ToString()
                tmpTable.Del_Address4 = oRow.Item("Del_Address4").ToString()
                tmpTable.Del_Address5 = oRow.Item("Del_Address5").ToString()
                tmpTable.Dispatch_Instructions = oRow.Item("Dispatch_Instructions").ToString()
                tmpTable.Operations_Instructions = oRow.Item("Operations_Instructions").ToString()
                tmpTable.Type = oRow.Item("Type").ToString()
                db.Dispatch_Templates.InsertOnSubmit(tmpTable)
                db.SubmitChanges()

    Sir , your proposed answered above was actually correct and would work well in C# windows Forms!!

    had to involve some INner joining tables because of the structure of my program and what it needs to do.....

    Instead of using the text boxes I'm updating the grid directly as the user enters values that need to be stored on the DB table.

    Learnt something....

    Thanks for Your Effort

    and Kind Regards

    Wednesday, August 21, 2013 1:35 PM