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. GridView databinding event handler

GridView databinding event handler

Scheduled Pinned Locked Moved ASP.NET
helptutorialgraphicssysadminquestion
3 Posts 2 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.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hi All, I'm having a problem with a gridview that I am dynamically creating with my code behind. I have a multiline textbox for users to input data that is then displayed in the gridview. When the gridview databinds with the table, it is not displaying the new lines. I've found a lot of articles online showing how to fix this by setting HTMLEncode = "false", however because I'm not using any bound fields I don't think that applies for this situation. I'm thinking I might be able to resolve this by catching the text being read in from the file during the databind and doing a Text.Replace(Environment.NewLine, "
    ") which would display them correctly in the cells without actually modifying the saved text in my saved file on the server.

    GridView gv1 = new GridView();
    gv1.Width = 600;
    gv1.DataSource = Table1;
    gv1.DataBind();

        gv1.HeaderRow.Height = 50;
        gv1.HeaderRow.BackColor = System.Drawing.Color.Black;
        gv1.HeaderRow.ForeColor = System.Drawing.Color.White;
        gv1.RowStyle.Height = 100;
    

    Can someone give me a quick example of how I could wire this up in the code behind? Thanks!

    "You're damned if you do, and you're damned if you dont" - Bart Simpson

    C 1 Reply Last reply
    0
    • C compninja25

      Hi All, I'm having a problem with a gridview that I am dynamically creating with my code behind. I have a multiline textbox for users to input data that is then displayed in the gridview. When the gridview databinds with the table, it is not displaying the new lines. I've found a lot of articles online showing how to fix this by setting HTMLEncode = "false", however because I'm not using any bound fields I don't think that applies for this situation. I'm thinking I might be able to resolve this by catching the text being read in from the file during the databind and doing a Text.Replace(Environment.NewLine, "
      ") which would display them correctly in the cells without actually modifying the saved text in my saved file on the server.

      GridView gv1 = new GridView();
      gv1.Width = 600;
      gv1.DataSource = Table1;
      gv1.DataBind();

          gv1.HeaderRow.Height = 50;
          gv1.HeaderRow.BackColor = System.Drawing.Color.Black;
          gv1.HeaderRow.ForeColor = System.Drawing.Color.White;
          gv1.RowStyle.Height = 100;
      

      Can someone give me a quick example of how I could wire this up in the code behind? Thanks!

      "You're damned if you do, and you're damned if you dont" - Bart Simpson

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I'd imagine an onitemdatabound event handler, and you'd find the textbox in each row and replace the NewLine with a br tag.

      Christian Graus Driven to the arms of OSX by Vista.

      C 1 Reply Last reply
      0
      • C Christian Graus

        I'd imagine an onitemdatabound event handler, and you'd find the textbox in each row and replace the NewLine with a br tag.

        Christian Graus Driven to the arms of OSX by Vista.

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        Hey Christian, I really like your Quote "Driven to the arms of OSX by Vista". I'm about to that point myself using Vista x64, however I might go for one of the Linux flavors ;) Do you have an example of how I would find the textbox in each row? I'm creating the gridview dynamically because the columns will change depending on the date each week.

        GridView gv1 = new GridView();
        gv1.Width = 600;
        gv1.DataSource = Table1;
        gv1.DataBind();

            gv1.HeaderRow.Height = 50;
            gv1.HeaderRow.BackColor = System.Drawing.Color.Black;
            gv1.HeaderRow.ForeColor = System.Drawing.Color.White;
            gv1.RowStyle.Height = 100;
        
            GridView gv2 = new GridView();
            gv2.Width = 780;        
            gv2.DataSource = Table2;
            gv2.DataBind();
                    
            switch (DateTime.Today.DayOfWeek.ToString())
            {
                case "Monday":
                    gv2.Rows\[0\].Cells\[0\].BackColor = System.Drawing.Color.Aqua;
                    break;
                case "Tuesday":
                    gv2.Rows\[0\].Cells\[1\].BackColor = System.Drawing.Color.Aqua;
                    break;
                case "Wednesday":
                    gv2.Rows\[0\].Cells\[2\].BackColor = System.Drawing.Color.Aqua;
                    break;
                case "Thursday":
                    gv2.Rows\[0\].Cells\[3\].BackColor = System.Drawing.Color.Aqua;
                    break;
                case "Friday":
                    gv2.Rows\[0\].Cells\[4\].BackColor = System.Drawing.Color.Aqua;
                    break;
            }
            
            gv2.HeaderRow.Height = 50;
            gv2.HeaderRow.BackColor = System.Drawing.Color.Black;
            gv2.HeaderRow.ForeColor = System.Drawing.Color.White;
            gv2.RowStyle.Height = 175;       
        
            GridView gv3 = new GridView();
            gv3.Width = 600;
            gv3.DataSource = Table3;
            gv3.DataBind();
            
            gv3.HeaderRow.Height = 50;
            gv3.HeaderRow.BackColor = System.Drawing.Color.Black;
            gv3.HeaderRow.ForeColor = System.Drawing.Color.White;
            gv3.RowStyle.Height = 100;        
        
            Panel1.Controls.Add(gv1);        
            Panel1.Controls.Add(gv2);
            Panel1.Controls.Add(gv3);
        

        "You're damned if you do, and you're damned if you dont" - Bart Simpson

        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