Should I write my own implementation of GridView?

I havenโ€™t worked with GridView too much, and after you tricked him, find him more complex than I need, but missed out on some very basic abilities that I would expect from him. Undoubtedly, its implementation makes sense, given that its 90% of the time is associated with binding to a data set, especially declaratively, but I intend to bind it to IEnumerable<T>in the code.

I need the ability to easily do the following

  • a) are tied to IEnumerable<T>, where columns can be limited only by certain properties of the typeT
  • b) is requested to return a collection of its rows, where each row can have a cell that looked for a property that was bound to

in principle, something that implements the following interface would be nice

public interface IEasyGridBinder {
  void Bind<T>(IEnumerable<T> bindableObjects, params string[] propertiesToBind);
  IList<IDictionary<string, string>> Values {get;}
}

To get this, do I have to write my own custom EasyGridBinder that inherits from the GridView and implements this interface, or is there really a simple way to do this that I'm just not familiar with?

PS Bonus points if I can write something like

myGrid.Bind(myEntities, e=>{e.Id; e.Name; e.Customer.Name;});

But I suppose I can figure it out after reading the expressions

Follow-up question: Is there no way to get the raw data that was entered into gridview and not converted to html? If the field received as input contains an empty string, the cell seems to contain "" so is there no way to distinguish between entering an empty string and a space? If this is true, then I will probably return to the implementation of most of the GridView functions.

+3
3

LinqDataSource . GridView . .aspx, , bloat GridView.

+2

GridViews AutoGenerateColumns false, . , BoundFields Gridview Columns.

GridView gv = new GridView();
gv.AutoGenerateColumns = false;

BoundField bf = new BoundField();
bf.DataField = "Id";
bf.HeaderText = "ID";
gv.Columns.Add(bf);

BoundField bf = new BoundField();
bf.DataField = "Name";
bf.HeaderText = "Name";
gv.Columns.Add(bf);

BoundField bf = new BoundField();
bf.DataField = "Customer.Name";
bf.HeaderText = "Customer Name";
gv.Columns.Add(bf);

gv.DataSource = IEnumerable<T>;
gv.DataBind();

, , , , :

, GridViewDisplayAttribute, . GridViewDisplayAttribute HeaderText. T, HeaderText. T, BoundFields , HeaderText.

:

using System;
public class GridViewDisplayAttribute : Attribute
{
public GridViewDisplayAttribute(string headerText)
{
        HeaderText = headerText;
}
    public readonly bool HeaderText;
}

GridView gv = new GridView();
gv.AutoGenerateColumns = false;

Type t = <T>.GetType();
PropertyInfo[] pis = t.GetProperties();

foreach (PropertyInfo pi in pis)
{
    GridViewDisplayAttribute[] gvdaArray = pi.GetCustomAttributes(
        typeof(GridViewDisplayAttribute), true);

    foreach (GridViewDisplayAttribute gvda in gvdaArray)
    {
        BoundField bf = new BoundField();
        bf.DataField = pi.Name;
        bf.HeaderText = gvda.HeaderText;
    }

    gv.Columns.Add(bf);
}

gv.DataSource = IEnumerable<T>;
gv.DataBind();
+1

I would recommend using extension methods to add the required behavior. The only drawback to this is that you cannot add โ€œValuesโ€ as a property.

0
source

Source: https://habr.com/ru/post/1704613/


All Articles