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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. get row number after datagrid sort

get row number after datagrid sort

Scheduled Pinned Locked Moved C#
cssalgorithmshelp
3 Posts 2 Posters 2 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.
  • J Offline
    J Offline
    jagberg23
    wrote on last edited by
    #1

    Hi, Ive tried a Datagrid datasource bound to a dataview and dataset. I click on the header and it sorts the grid, which i want. After this i want to get the value in the first row, first column of the new datagrid before it has been drawn. Before I sort the grid i get the value either by getting it from the datasource or the dataview and it is what I want. Now i try to get the value after the sort and it gives me the same value as before the sort was done. Ive tried to bind the new dataview after programatically sorting it and getting the value, but with no luck. Please help. My aim before was to override the Paint method and color the rows I want a different colour. So i need to check the value before it paints.

    A 1 Reply Last reply
    0
    • J jagberg23

      Hi, Ive tried a Datagrid datasource bound to a dataview and dataset. I click on the header and it sorts the grid, which i want. After this i want to get the value in the first row, first column of the new datagrid before it has been drawn. Before I sort the grid i get the value either by getting it from the datasource or the dataview and it is what I want. Now i try to get the value after the sort and it gives me the same value as before the sort was done. Ive tried to bind the new dataview after programatically sorting it and getting the value, but with no luck. Please help. My aim before was to override the Paint method and color the rows I want a different colour. So i need to check the value before it paints.

      A Offline
      A Offline
      aplope
      wrote on last edited by
      #2

      There are two different methods that can be used to do this. (Please be sure to check MSDN on the datagrid to get more details about both). I color the column (not a row) that has just been sorted in an "OnItemCreated" procedure. I check to make sure the item is an Item or an Alternating Item and then I get the back color like this:

      e.Item.Cells[m_iSortColumnIdx].BackColor = (System.Drawing.Color) new ColorConverter().ConvertFromString("#CCFFCC");

      The other procedure you can use is the "OnItemDatabound" procedure. Again, you have to check to make sure the item is an Item or an Alternating Item. From this method, you can access each of the cells and a number of attributes for that cell: text, back color, fore color, alignment, columnspan, class, etc. So if you know the column number (zero-based), you can also get the text. Here's how I format my social security numbers in my very first column of my datagrid.

      e.Item.Cells[0].Text = Utils.FormatSocial( (string)DataBinder.Eval(e.Item.DataItem, "social_num") );

      I believe if you wanted to color the whole row, you might need to loop through each of the cells and set the color in the loop. (From what I remember of trying to do this in the past, this is how I had to change the style class or the color for an entire row). You might also try 4 Guys From Rolla which is running an EXTENSIVE examination of the datagrid. You might find some discussion of these methods in that set of articles. The latest is # 17. Pegmeister

      J 1 Reply Last reply
      0
      • A aplope

        There are two different methods that can be used to do this. (Please be sure to check MSDN on the datagrid to get more details about both). I color the column (not a row) that has just been sorted in an "OnItemCreated" procedure. I check to make sure the item is an Item or an Alternating Item and then I get the back color like this:

        e.Item.Cells[m_iSortColumnIdx].BackColor = (System.Drawing.Color) new ColorConverter().ConvertFromString("#CCFFCC");

        The other procedure you can use is the "OnItemDatabound" procedure. Again, you have to check to make sure the item is an Item or an Alternating Item. From this method, you can access each of the cells and a number of attributes for that cell: text, back color, fore color, alignment, columnspan, class, etc. So if you know the column number (zero-based), you can also get the text. Here's how I format my social security numbers in my very first column of my datagrid.

        e.Item.Cells[0].Text = Utils.FormatSocial( (string)DataBinder.Eval(e.Item.DataItem, "social_num") );

        I believe if you wanted to color the whole row, you might need to loop through each of the cells and set the color in the loop. (From what I remember of trying to do this in the past, this is how I had to change the style class or the color for an entire row). You might also try 4 Guys From Rolla which is running an EXTENSIVE examination of the datagrid. You might find some discussion of these methods in that set of articles. The latest is # 17. Pegmeister

        J Offline
        J Offline
        jagberg23
        wrote on last edited by
        #3

        hi, Thanks for the help. I will try this out, its something I can do after my grid has been paited but I still need a way of getting the row information before the grid has been painted. What I do is check if something exists in the row and if it does I color it a certain color. My problem is that the paint method has the row number as a parameter but when I get the row from the Datasource bound to the grid using this row it doesnt give me the row that actually displays. This happens after I click the column header to sort the grid. I cant cast the CurrencyManager Source and check its details because it only shows information for a specific cell in the row (I think its GetValueAtColumn(Source, RowNum)). I need to check the 1st column in that row and determine what colour to paint the cell.

        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