Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Insert from GridView to DetailsView record

Insert from GridView to DetailsView record

Scheduled Pinned Locked Moved ASP.NET
questiondatabasealgorithmstutorial
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kmeyer
    wrote on last edited by
    #1

    Hi All, Probably seems like a stupid question but how do I pass something like a CustomerID value from a GridView master record to an attached DetailsView form when creating a new details record in Insert mode. I have been searching for a good example and can only find samples working with existing records in both the Master and Detail tables. What I am trying to do is open the DetailsView form in Insert mode perhaps with a LoadForm event or similar when the attached query returns no detail records and automatically pass the key record value for a new details record from the GridVew. I do not want the user inputting this information and would also like to possibly insert other values into the details record from the selected master in the GridView at the same time. :~ Thanks in advance Kim

    M 1 Reply Last reply
    0
    • K kmeyer

      Hi All, Probably seems like a stupid question but how do I pass something like a CustomerID value from a GridView master record to an attached DetailsView form when creating a new details record in Insert mode. I have been searching for a good example and can only find samples working with existing records in both the Master and Detail tables. What I am trying to do is open the DetailsView form in Insert mode perhaps with a LoadForm event or similar when the attached query returns no detail records and automatically pass the key record value for a new details record from the GridVew. I do not want the user inputting this information and would also like to possibly insert other values into the details record from the selected master in the GridView at the same time. :~ Thanks in advance Kim

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      You can create a handler for the RowDataBound event of the GridView control, and in the handler you can get reference to the DetailsView control, then you can pass the CustomerID value from the master record to the input controls placed inside the DetailsView control.

      K 1 Reply Last reply
      0
      • M minhpc_bk

        You can create a handler for the RowDataBound event of the GridView control, and in the handler you can get reference to the DetailsView control, then you can pass the CustomerID value from the master record to the input controls placed inside the DetailsView control.

        K Offline
        K Offline
        kmeyer
        wrote on last edited by
        #3

        Thankyou for the reply, it would seem from the number of posts on various ASP.NET forums I am not alone. I am not alone, I will explore the RowDataBound event as a means to capture the values in the GridView (CustomerID & Name) I thought this was going to be easy, hooking up a Gridview and DetailsView for some standard Master/Detail data entry. For anyone reading this post if you can recommend a good book that focuses on C# ASP.NET database front ends I would appreciate knowing its name. I have already purchased 3 books reviewed on the Microsoft site, one is over 1000 pages only find very little on hooking up these two controls in a real world examples. I have both forms linked and working well when details data exists. I also have the DetailsView defaulting to Insert by using the following useful code snippet I found on another post, when there is no detail record. protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e) { if (e.AffectedRows < 1) { DetailsView1.ChangeMode(DetailsViewMode.Insert); } } Its at this point I want to populate the CustomerId & Name values from the selected Master record into the empty DetailsView record which is on a different page. Many smaller examples show both controls on the same page. I have tried the following from a post by Polita Paulas the creator of the GridView, DetailsView and FormView controls. "The best way to do this is to handle the DataBound event, check if the Mode is Insert, then set the text in the controls to the default properties. For instance: protected void DetailsView1_DataBound(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Insert) { ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).Text = "Default"; } } " I have tried this with test data and cannot seem to get it to work, the Cell always remains empty, no errors are thrown. A number of posts suggest you must use template fields, like the following post, I could not get this to work either. protected void DetailsView1_DataBound(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Insert) { TextBox tb = (TextBox)DetailsView1.FindControl("TextBox1"); tb.Text = DateTime.Now.ToShortDateString(); } } I am thinking this must be so easy and thats why my posts on this site and another go vertually unanswered or its too hard and no one really knows.

        M 1 Reply Last reply
        0
        • K kmeyer

          Thankyou for the reply, it would seem from the number of posts on various ASP.NET forums I am not alone. I am not alone, I will explore the RowDataBound event as a means to capture the values in the GridView (CustomerID & Name) I thought this was going to be easy, hooking up a Gridview and DetailsView for some standard Master/Detail data entry. For anyone reading this post if you can recommend a good book that focuses on C# ASP.NET database front ends I would appreciate knowing its name. I have already purchased 3 books reviewed on the Microsoft site, one is over 1000 pages only find very little on hooking up these two controls in a real world examples. I have both forms linked and working well when details data exists. I also have the DetailsView defaulting to Insert by using the following useful code snippet I found on another post, when there is no detail record. protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e) { if (e.AffectedRows < 1) { DetailsView1.ChangeMode(DetailsViewMode.Insert); } } Its at this point I want to populate the CustomerId & Name values from the selected Master record into the empty DetailsView record which is on a different page. Many smaller examples show both controls on the same page. I have tried the following from a post by Polita Paulas the creator of the GridView, DetailsView and FormView controls. "The best way to do this is to handle the DataBound event, check if the Mode is Insert, then set the text in the controls to the default properties. For instance: protected void DetailsView1_DataBound(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Insert) { ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).Text = "Default"; } } " I have tried this with test data and cannot seem to get it to work, the Cell always remains empty, no errors are thrown. A number of posts suggest you must use template fields, like the following post, I could not get this to work either. protected void DetailsView1_DataBound(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Insert) { TextBox tb = (TextBox)DetailsView1.FindControl("TextBox1"); tb.Text = DateTime.Now.ToShortDateString(); } } I am thinking this must be so easy and thats why my posts on this site and another go vertually unanswered or its too hard and no one really knows.

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          If the Creator said the DataBound event is the best way, then it should be be your choice :). There are a couple of things things that you might want to check: + Make sure the execution gets in the body of the if statement in the DetailsView1_DataBound event handler, I just want to ensure that your code does assign the default value to the input textbox. + Make sure you get reference to the right input textbox to set the default value. For example, make sure you do not assign a value to the input textbox of the BoundField or TemplateField with the InsertVisible property is set to false. + Make sure the Text property of the input textbox is not overwritten somewhere else. Basically, when you get reference to the textbox with the DefailsView is in the Insert mode, you'll have 2 common options: + Use the rowindex and cellindex values, for example when you use the BoundField in the DetailsView control. + Use the FindControl method, and you use this way when define a TemplateField in the DetailsView control. Perhaps, if you still cannot figure out the root cause, you might want to post your code snippets, that might help. In addition, I don't know off any book containing lots of examples about the GridView, DetailsView controls, but you may find online from the sites: http://www.gridviewgirl.com[^] http://www.gridviewguy.com/[^]

          K 1 Reply Last reply
          0
          • M minhpc_bk

            If the Creator said the DataBound event is the best way, then it should be be your choice :). There are a couple of things things that you might want to check: + Make sure the execution gets in the body of the if statement in the DetailsView1_DataBound event handler, I just want to ensure that your code does assign the default value to the input textbox. + Make sure you get reference to the right input textbox to set the default value. For example, make sure you do not assign a value to the input textbox of the BoundField or TemplateField with the InsertVisible property is set to false. + Make sure the Text property of the input textbox is not overwritten somewhere else. Basically, when you get reference to the textbox with the DefailsView is in the Insert mode, you'll have 2 common options: + Use the rowindex and cellindex values, for example when you use the BoundField in the DetailsView control. + Use the FindControl method, and you use this way when define a TemplateField in the DetailsView control. Perhaps, if you still cannot figure out the root cause, you might want to post your code snippets, that might help. In addition, I don't know off any book containing lots of examples about the GridView, DetailsView controls, but you may find online from the sites: http://www.gridviewgirl.com[^] http://www.gridviewguy.com/[^]

            K Offline
            K Offline
            kmeyer
            wrote on last edited by
            #5

            Thankyou for your patience, I will keep chipping away at it and take on board your suggestions. I have been trolling the Net on and off for a couple weeks and am pleased I am not the only one in this prediciment. Both the Gridviewguy and Gridviewgirl sites are good and I have seen posts from Gridviewguy assisting on forums but unless I missed something I could not see an example of what I am doing. Scott Mitchell (www.4guysfromrolla.com) also has contributed some good examples to the Microsoft MSDN site at the following link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/GridViewEx.asp Scott has also contributed a number of good articles for Microsoft under learn at www.asp.net There are 13 articles with more coming and can they be downloaded as PDF files At the moment I am compiling eveything I have trolled up and will go through it again in case I have missed something. I am trying to teach myself C# ASP.NET whilst building an in house application. My previous backgroud dates back to the xbase dos pre Windows days and then some early db work in Delphi and object pascal, so I am starting from scratch. Thanks Again Kim

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups