(495) 925-0049, ITShop - 229-0436, 925-0049
 

 
 
 
 
 

ASP.NET: GridView

. -. , MVC , Web Forms . Web Forms, . (, TextBox) , , , - .
, GridView . , .
.


. GridView

, , - . GridView , .
- (, DataSourceControl), , DataSource. .

, : , GridView, , ( / ). .

:

  • -
  • ()
  • GridViewList.aspx,
  • GridViewList.aspx

, . -, , :

  1. [Serializable]
  2. public class People
  3. {
  4. [XmlAttribute]
  5. public int Id { get; set; }
  6. [XmlAttribute]
  7. public string FirstName { get; set; }
  8. [XmlAttribute]
  9. public string LastName { get; set; }
  10. }

. -, , ViewState. , .

  1. public List<People> Peoples
  2. {
  3. get
  4. {
  5. // ViewState.
  6. // hfPeoplesViewState - HiddenField,
  7. // , .
  8. // var str = "Peoples_Key"
  9. var str = hfPeoplesViewState.Value;
  10. if (string.IsNullOrEmpty(str))
  11. {
  12. // ,
  13. hfPeoplesViewState.Value = string.Format("Peoples_{0}", Guid.NewGuid());
  14. str = hfPeoplesViewState.Value;
  15. }
  16. // , ViewState , -
  17. if (ViewState[str] == null // !(ViewState[str] is List<People>))
  18. {
  19. var peoples = new List<People>
  20. {
  21. new People {Id = 1, LastName = "", FirstName = ""},
  22. new People {Id = 2, LastName = "", FirstName = ""},
  23. new People {Id = 3, LastName = "", FirstName = ""}
  24. };
  25. ViewState[str] = peoples;
  26. return peoples;
  27. }
  28. return ViewState[str] as List<People>;
  29. }
  30. set
  31. {
  32. // get
  33. var str = hfPeoplesViewState.Value;
  34. if (string.IsNullOrEmpty(str))
  35. {
  36. hfPeoplesViewState.Value = string.Format("Peoples_{0}", Guid.NewGuid());
  37. str = hfPeoplesViewState.Value;
  38. }
  39. ViewState[str] = value;
  40. }
  41. }

GridView . , .

  1. <asp:TextBox runat="server" ID="tbFirstName">
  2. </asp:TextBox>
  3. <asp:TextBox runat="server" ID="tbLastName">
  4. </asp:TextBox>
  5. <asp:Button runat="server" OnClick="BtAddPeople" Text="+" />
  6. <hr />
  7. <asp:GridView runat="server" ID="gv" AutoGenerateColumns="True" Width="60%">
  8. <EmptyDataTemplate>
  9. </EmptyDataTemplate>
  10. </asp:GridView>

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. gv.DataSource = Peoples;
  6. gv.DataBind();
  7. }
  8. }
  9. protected void BtAddPeople(object sender, EventArgs e)
  10. {
  11. if (!string.IsNullOrEmpty(tbFirstName.Text) && !string.IsNullOrEmpty(tbLastName.Text))
  12. {
  13. var id = Peoples.Count > 0 ? Peoples.Max(x => x.Id) + 1 : 1;
  14. var p = new People
  15. {
  16. Id = id,
  17. FirstName = tbFirstName.Text,
  18. LastName = tbLastName.Text
  19. };
  20. Peoples.Add(p);
  21. UpdateGrid();
  22. }
  23. }
  24. private void UpdateGrid()
  25. {
  26. gv.DataSource = Peoples;
  27. gv.DataBind();
  28. }

, , GridView. , GridView . :

, , , , . . , , ( ):

  1. <asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" Width="60%" AllowSorting="True">
  2. <Columns>
  3. <asp:BoundField HeaderText="" DataField="Id" SortExpression="Id" ReadOnly="True">
  4. <ItemStyle Width="5%"></ItemStyle>
  5. </asp:BoundField>
  6. <asp:BoundField HeaderText="" DataField="FirstName" SortExpression="FirstName">
  7. <ItemStyle Width="40%"></ItemStyle>
  8. </asp:BoundField>
  9. <asp:BoundField HeaderText="" DataField="LastName" SortExpression="LastName">
  10. <ItemStyle Width="40%"></ItemStyle>
  11. </asp:BoundField>
  12. <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" ShowCancelButton="True">
  13. <ItemStyle Width="15%"></ItemStyle>
  14. </asp:CommandField>
  15. </Columns>
  16. <EmptyDataTemplate>
  17. </EmptyDataTemplate>
  18. </asp:GridView>

, , , . . GridView . , . : OnRowEditing="GvEditing" OnRowUpdating="GvUpdating" OnRowCancelingEdit="GvCancelingEdit", OnSorting="GvSorting", OnRowDeleting="GvDeleting".

  1. <asp:GridView runat="server" ID="gv" AllowSorting="True" OnSorting="GvSorting" OnRowEditing="GvEditing"
  2. OnRowUpdating="GvUpdating" OnRowCancelingEdit="GvCancelingEdit" OnRowDeleting="GvDeleting"
  3. DataKeyNames="Id" AutoGenerateColumns="False" Width="60%" >
  4. <Columns>
  5. <asp:BoundField HeaderText="" DataField="Id" SortExpression="Id" ReadOnly="True">
  6. <ItemStyle Width="5%"></ItemStyle>
  7. </asp:BoundField>
  8. <asp:BoundField HeaderText="" DataField="FirstName" SortExpression="FirstName">
  9. <ItemStyle Width="40%"></ItemStyle>
  10. </asp:BoundField>
  11. <asp:BoundField HeaderText="" DataField="LastName" SortExpression="LastName">
  12. <ItemStyle Width="40%"></ItemStyle>
  13. </asp:BoundField>
  14. <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" ShowCancelButton="True">
  15. <ItemStyle Width="15%"></ItemStyle>
  16. </asp:CommandField>
  17. </Columns>
  18. <EmptyDataTemplate>
  19. </EmptyDataTemplate>
  20. </asp:GridView>

  1. /// <summary>
  2. /// .
  3. /// </summary>
  4. protected void GvSorting(object sender, GridViewSortEventArgs e)
  5. {
  6. var p = Peoples;
  7. // ,
  8. if (e.SortExpression == "LastName")
  9. {
  10. // Sort
  11. p = Sort("LastName", list => list.OrderBy(x => x.LastName).ToList(),
  12. list => list.OrderByDescending(x => x.LastName).ToList(), p);
  13. }
  14. if (e.SortExpression == "FirstName")
  15. {
  16. p = Sort("FirstName", list => list.OrderBy(x => x.FirstName).ToList(),
  17. list => list.OrderByDescending(x => x.FirstName).ToList(), p);
  18. }
  19. if (e.SortExpression == "Id")
  20. {
  21. p = Sort("Id", list => list.OrderBy(x => x.Id).ToList(),
  22. list => list.OrderByDescending(x => x.Id).ToList(), p);
  23. }
  24. Peoples = p;
  25. //
  26. UpdateGrid();
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. private List<People> Sort(string column, Func<List<People>, List<People>> ascFunc, Func<List<People>, List<People>> descFunc, List<People> data)
  32. {
  33. List<People> result;
  34. //
  35. if (hfLastSortFieldState.Value == column)
  36. {
  37. // ,
  38. result = hfLastSortDirectionState.Value != "ASC" ? ascFunc(data) : descFunc(data);
  39. //
  40. hfLastSortDirectionState.Value = hfLastSortDirectionState.Value != "ASC" ? "ASC" : "DESC";
  41. hfLastSortFieldState.Value = column;
  42. }
  43. else
  44. {
  45. // ,
  46. result = ascFunc(data);
  47. //
  48. hfLastSortDirectionState.Value = "ASC";
  49. hfLastSortFieldState.Value = column;
  50. }
  51. return result;
  52. }

  1. /// <summary>
  2. /// . Id ,
  3. /// DataKeyNames="Id"
  4. ///
  5. /// </summary>
  6. protected void GvDeleting(object sender, GridViewDeleteEventArgs e)
  7. {
  8. int id;
  9. //
  10. if (int.TryParse(string.Format("{0}", e.Keys["Id"]), out id))
  11. {
  12. //
  13. Peoples = Peoples.Where(x => x.Id != id).ToList();
  14. //
  15. UpdateGrid();
  16. }
  17. }
  18. /// <summary>
  19. /// , .
  20. ///
  21. /// </summary>
  22. protected void GvEditing(object sender, GridViewEditEventArgs e)
  23. {
  24. gv.EditIndex = e.NewEditIndex;
  25. UpdateGrid();
  26. }
  27. /// <summary>
  28. /// , .
  29. /// Id , Id,
  30. /// </summary>
  31. protected void GvUpdating(object sender, GridViewUpdateEventArgs e)
  32. {
  33. int id;
  34. if (int.TryParse(string.Format("{0}", e.Keys["Id"]), out id))
  35. {
  36. Peoples
  37. .Where(x => x.Id == id)
  38. .ToList()
  39. .ForEach(x =>
  40. {
  41. // string.Format,
  42. // e.NewValues["FirstName"] object
  43. // null
  44. x.FirstName = string.Format("{0}", e.NewValues["FirstName"]);
  45. x.LastName = string.Format("{0}", e.NewValues["LastName"]);
  46. });
  47. gv.EditIndex = -1;
  48. UpdateGrid();
  49. }
  50. }
  51. /// <summary>
  52. /// ,
  53. /// gv.EditIndex = -1;
  54. /// </summary>
  55. protected void GvCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  56. {
  57. gv.EditIndex = -1;
  58. UpdateGrid();
  59. }

, , , .

, , ? (, DataSourceControl) . ObjectDataSource. ObjectDataSource, , - , , , .

. , , ObjectDataSource. , - ( , , , ).

,

  1. public class Car
  2. {
  3. public int Id { get; set; }
  4. public string Manufacturer { get; set; }
  5. public string Model { get; set; }
  6. }

-. ObjectDataSource . , ObjectDataSource. :

  1. public class CarRepository
  2. {
  3. /// <summary>
  4. /// .
  5. /// </summary>
  6. private static List<Car> _store;
  7. /// <summary>
  8. /// .
  9. /// </summary>
  10. static CarRepository()
  11. {
  12. _store = new List<Car>();
  13. for (var i = 0; i < 1000; i++)
  14. {
  15. _store.Add(new Car
  16. {
  17. Id = i,
  18. Manufacturer = "Manufacturer" + i,
  19. Model = "Model" + i
  20. });
  21. }
  22. }
  23. /// <summary>
  24. /// . . ,
  25. /// . ,
  26. /// "Id". sort "Id".
  27. /// - "Id DESC"
  28. /// </summary>
  29. public IEnumerable<Car> GetCars(int maximumRows, int startRowIndex, string sort)
  30. {
  31. IEnumerable<Car> temp = _store;
  32. // , ,
  33. if (sort.Contains("DESC"))
  34. {
  35. if (sort.Contains("Id")) temp = _store.OrderByDescending(x => x.Id);
  36. if (sort.Contains("Manufacturer")) temp = _store.OrderByDescending(x => x.Manufacturer);
  37. if (sort.Contains("Model")) temp = _store.OrderByDescending(x => x.Model);
  38. }
  39. else
  40. {
  41. if (sort.Contains("Id")) temp = _store.OrderBy(x => x.Id);
  42. if (sort.Contains("Manufacturer")) temp = _store.OrderBy(x => x.Manufacturer);
  43. if (sort.Contains("Model")) temp = _store.OrderBy(x => x.Model);
  44. }
  45. return temp.Skip(startRowIndex).Take(maximumRows);
  46. }
  47. /// <summary>
  48. /// .
  49. ///
  50. /// </summary>
  51. public int Count()
  52. {
  53. return _store.Count;
  54. }
  55. /// <summary>
  56. /// Car
  57. ///
  58. /// </summary>
  59. public void Update(Car car)
  60. {
  61. _store
  62. .Where(x => x.Id == car.Id)
  63. .ToList()
  64. .ForEach(x =>
  65. {
  66. x.Manufacturer = car.Manufacturer;
  67. x.Model = car.Model;
  68. });
  69. }
  70. /// <summary>
  71. /// . .
  72. /// </summary>
  73. public void Delete(Car car)
  74. {
  75. _store = _store.Where(x => x.Id != car.Id).ToList();
  76. }
  77. /// <summary>
  78. ///
  79. /// </summary>
  80. public void Insert(Car car)
  81. {
  82. car.Id = _store.Count > 0 ? _store.Max(x => x.Id) + 1 : 1;
  83. _store.Add(car);
  84. }
  85. }

. , , 2 ( )

  1. <asp:TextBox runat="server" ID="tbManufacturer">
  2. </asp:TextBox>
  3. <asp:TextBox runat="server" ID="tbModel">
  4. </asp:TextBox>
  5. <asp:Button runat="server" OnClick="BtAddCar" Text="+" />
  6. <hr />

  1. protected void BtAddCar(object sender, EventArgs e)
  2. {
  3. if (!string.IsNullOrEmpty(tbManufacturer.Text) && !string.IsNullOrEmpty(tbModel.Text))
  4. {
  5. var carRepository = new CarRepository();
  6. carRepository.Insert(new Car {Manufacturer = tbManufacturer.Text, Model = tbModel.Text});
  7. gv.DataBind();
  8. }
  9. }

ObjectDataSource.

  1. <asp:ObjectDataSource ID="ods" runat="server" TypeName="GridViewTest.CarRepository"
  2. DataObjectTypeName="GridViewTest.Car" EnablePaging="True" MaximumRowsParameterName="maximumRows"
  3. StartRowIndexParameterName="startRowIndex" SortParameterName="sort" InsertMethod="Insert"
  4. SelectCountMethod="Count" SelectMethod="GetCars" UpdateMethod="Update" DeleteMethod="Delete">
  5. </asp:ObjectDataSource>

.

, . .

  1. <asp:GridView runat="server" ID="gv" AllowSorting="True" DataKeyNames="Id" AutoGenerateColumns="False"
  2. DataSourceID="ods" Width="60%" AllowPaging="True" PageSize="20">
  3. <Columns>
  4. <%--, , BoundField--%>
  5. <asp:BoundField HeaderText="" DataField="Id" ReadOnly="True" SortExpression="Id">
  6. <ItemStyle Width="5%"></ItemStyle>
  7. </asp:BoundField>
  8. <%-- BoundField, ,
  9. TemplateField (Eval) (Bind) --%>
  10. <asp:TemplateField HeaderText="" SortExpression="Manufacturer">
  11. <ItemStyle Width="35%"></ItemStyle>
  12. <ItemTemplate>
  13. <%#Eval("Manufacturer")%>
  14. </ItemTemplate>
  15. <EditItemTemplate>
  16. <asp:TextBox runat="server" ID="tbMan" Text='<%#Bind("Manufacturer") %>'></asp:TextBox>
  17. </EditItemTemplate>
  18. </asp:TemplateField>
  19. <%-- --%>
  20. <asp:TemplateField HeaderText="" SortExpression="Model">
  21. <ItemStyle Width="35%"></ItemStyle>
  22. <ItemTemplate>
  23. <%#Eval("Model")%>
  24. </ItemTemplate>
  25. <EditItemTemplate>
  26. <asp:TextBox runat="server" ID="tbModel" Text='<%#Bind("Model") %>'></asp:TextBox>
  27. </EditItemTemplate>
  28. </asp:TemplateField>
  29. <%-- , , /,
  30. CommandField.
  31. CommandName --%>
  32. <asp:TemplateField>
  33. <ItemStyle Width="25%"></ItemStyle>
  34. <ItemTemplate>
  35. <asp:LinkButton runat="server" CommandName="Edit" Text=""></asp:LinkButton>
  36. <%-- , . ,
  37. OnClientClick , ,
  38. , --%>
  39. <asp:LinkButton runat="server" CommandName="Delete" Text=""
  40. OnClientClick="return confirm(' ?');"></asp:LinkButton>
  41. </ItemTemplate>
  42. <EditItemTemplate>
  43. <asp:LinkButton runat="server" CommandName="Update" Text=""></asp:LinkButton>
  44. <asp:LinkButton runat="server" CommandName="Cancel" Text=""></asp:LinkButton>
  45. </EditItemTemplate>
  46. </asp:TemplateField>
  47. </Columns>
  48. <%--, , --%>
  49. <EmptyDataTemplate>
  50. </EmptyDataTemplate>
  51. </asp:GridView>

, :

. , , .

, GridView , .



  »
   »
   
  » : 31.01.2012 
 

   WWW.ITSHOP.RU
SAP Crystal Presentation Design 2016 WIN INTL NUL
Inventory 9
IBM Domino Messaging Client Access License Authorized User License + SW Subscription & Support 12 Months
The BAT! Home Upgrade- 1
TeeGrid VCL/FMX Source Code single license
 
...
 
   WWW.ITSHOP.RU
 
...
 
   WWW.ITSHOP.RU
 
...
 
3D | 3D    WWW.ITSHOP.RU
 
...
 
 
 Subscribe.ru
: CASE, RAD, ERP, OLAP
ITShop.ru - , , ,
OS Linux . + + +
Visual ++
Visual Basic/Visual Studio ASP/ASP.NET
Windows
 
 
Download
 
 
 
"" Windows 7 (2)
, ...
 
Wordpad? (4)
Wordpad ? , , , . ...
 
Excel ? (57)
, , Excel , ...
 
" " MS Excel (4)
" " ...
 
Access, , (9)
, Access ( ),...
 
 
 



    
rambler's top100 Rambler's Top100