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. Catching the content of DataGrid at runtime

Catching the content of DataGrid at runtime

Scheduled Pinned Locked Moved ASP.NET
helpdatabasedocker
5 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    MrMicrosoftvn
    wrote on last edited by
    #1

    Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind: private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } } above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
    " before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
    "); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):)

    M J 2 Replies Last reply
    0
    • M MrMicrosoftvn

      Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind: private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } } above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
      " before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
      "); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):)

      M Offline
      M Offline
      Marcie Jones
      wrote on last edited by
      #2

      The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com

      A M 2 Replies Last reply
      0
      • M Marcie Jones

        The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        thanks for yr replying, before posting my problem i tried to use the ItemDataBound event so that i can not catch my content at runtime.

        1 Reply Last reply
        0
        • M Marcie Jones

          The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com

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

          thanks for yr replying, before posting my problem i tried to use the ItemDataBound event so that i can not catch my content at runtime.

          1 Reply Last reply
          0
          • M MrMicrosoftvn

            Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind: private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } } above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
            " before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
            "); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):)

            J Offline
            J Offline
            Jesse Squire
            wrote on last edited by
            #5

            Have you considered doing the string replacement at the database level? Unless you have a need for the "\n" version elsewere in your application, filtering in the query you use to populate the dataset would make your code cleaner and perform [a tiny bit] better. Hope that helps. :)   --Jesse

            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