code_junkie

How to format a number string in gridview?

Nov 12th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. // Handle the grid's RowDataBound event
  2. MyGridView.RowDataBound += new GridViewRowEventHandler(MyGridView_RowDataBound);
  3.  
  4. // Set the value to x / 1000 in the RowDataBound event
  5. protected void MyGridView_RowDataBound( object sender, GridViewRowEventArgs e )
  6. {
  7. if( e.Row.RowType != DataControlRowType.DataRow )
  8. return;
  9.  
  10. // Cast e.Row.DataItem to whatever type you're binding to
  11. BindingObject bindingObject = (BindingObject)e.Row.DataItem;
  12.  
  13. // Set the text of the correct column. Replace 0 with the position of the correct column
  14. e.Row.Cells[0].Text = bindingObject.ProcessingTime / 1000M;
  15. }
  16.  
  17. GridView.DataSource = ....
  18.  
  19. BoundField total = new BoundField();
  20. total.DataField = "Total";
  21. total.HeaderText = "Total Amount";
  22. total.DataFormatString = "{0:C}";
  23. donations.Columns.Add(total);
  24.  
  25. ......
  26.  
  27. String.Format("{0:0.000}", test/1000);
  28.  
  29. try
  30. {
  31. if (e.Row.RowType == DataControlRowType.DataRow)
  32. {
  33. if (((DataRowView)(e.Row.DataItem))["Columnname"].ToString().Equals("Total", StringComparison.OrdinalIgnoreCase))
  34. {
  35. e.Row.Font.Bold = true;
  36. //-----or ant thing you want to do------
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42.  
  43. }
Add Comment
Please, Sign In to add comment