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. General Programming
  3. .NET (Core and Framework)
  4. Style of a DataGridView.Row by using DataSet

Style of a DataGridView.Row by using DataSet

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
4 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
    C Scharbe
    wrote on last edited by
    #1

    The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName. DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0]; Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]

    modified on Friday, June 5, 2009 3:33 AM

    C L 2 Replies Last reply
    0
    • C C Scharbe

      The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName. DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0]; Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]

      modified on Friday, June 5, 2009 3:33 AM

      C Offline
      C Offline
      C Scharbe
      wrote on last edited by
      #2

      In that way i currently solved it: private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { int i = 0; try { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString().Contains(textBox1.Text)) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.ForestGreen; } int ii = 1 / i; } catch { } } I used the RowPrePaint event. So everytime when the row will be painted there is a check if the BackColor should be changed. I tested it with 1,000,000 rows in fullscreen and the performance is (not the best but) ok. But if you have another solution, feel free to post it.

      1 Reply Last reply
      0
      • C C Scharbe

        The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName. DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0]; Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]

        modified on Friday, June 5, 2009 3:33 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Instead of trying to set the background through code its better to use CSS.

        Regards Venkatesh Ellur Engineer - Software +919886417764 +919886235553

        C 1 Reply Last reply
        0
        • L Lost User

          Instead of trying to set the background through code its better to use CSS.

          Regards Venkatesh Ellur Engineer - Software +919886417764 +919886235553

          C Offline
          C Offline
          C Scharbe
          wrote on last edited by
          #4

          I forgot: It's a Windows.Forms application. So i can't do it in that way. But in Websites it's also a solution.

          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