Advertisement
DevUModerator

Untitled

Mar 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace GridViewButtonCommand
  9. {
  10. public partial class Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if(!Page.IsPostBack)
  15. {
  16. var cars = new List<Car>() {
  17. new Car { CarId=Guid.NewGuid(), Make="BMW", Model="528i", Year=2010 },
  18. new Car {CarId=Guid.NewGuid(), Make="Toyota", Model="4Runner", Year=2010},
  19. new Car {CarId=Guid.NewGuid(), Make="Hyundai", Model="Elantra", Year=2013}
  20. };
  21.  
  22. GridView1.DataSource = cars;
  23. GridView1.DataBind();
  24. }
  25.  
  26.  
  27.  
  28. }
  29.  
  30. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  31. {
  32. //e.CommandArgument
  33.  
  34. int index = Convert.ToInt32(e.CommandArgument);
  35. GridViewRow row = GridView1.Rows[index];
  36.  
  37. // this is a bit risky ... take great caution.
  38. var make = row.Cells[1].Text;
  39. var model = row.Cells[2].Text;
  40. var value = row.Cells[4].Text;
  41.  
  42. // You would probably want to convert it to its original type.
  43. var carId = Guid.Parse(value);
  44.  
  45. resultLabel.Text = String.Format("{0} {1} {2}",
  46. make,
  47. model,
  48. carId);
  49.  
  50. }
  51.  
  52.  
  53. }
  54.  
  55. public class Car
  56. {
  57. public Guid CarId { get; set; }
  58. public string Make { get; set; }
  59. public string Model { get; set; }
  60. public int Year { get; set; }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement