简体   繁体   English

我应该编写自己的GridView实现吗?

[英]Should I write my own GridView implementation?

I haven't worked with GridView too much and after poking around with it, find it to be more complex than I need but missing some very basic abilities that I would expect it to have. 我没有过多地使用过GridView,并且在使用它之后,发现它比我需要的更复杂,但却缺少一些我希望它具有的非常基本的功能。 No doubt its implementation makes sense given its 90% of the time purpose of being bound to a dataset especially when doing this declaratively, but I intend to bind it to an IEnumerable<T> in code. 毫无疑问,它的实现是有意义的,因为它的90%的时间目的是绑定到数据集,特别是在声明性地执行此操作时,但我打算将它绑定到代码中的IEnumerable<T>

What I need is the ability to do the following easily 我需要的是能够轻松完成以下任务

  • a) be bound to an IEnumerable<T> where the columns can be limited to only certain properties of type T a)绑定到IEnumerable<T> ,其中列可以仅限于T类型的某些属性
  • b) be queried to return a collection of its rows where each row can have a cell looked up by the property that cell was bound to b)被查询以返回其行的集合,其中每行可以具有由该单元被绑定到的属性查找的单元格

basically something implementing the following interface would be nice 基本上实现以下接口的东西会很好

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

So to get this, should I write my own custom EasyGridBinder that inherits from GridView and implements this interface or is there a really simple way to do these things that I am just unfamiliar with? 所以,为了得到这个,我应该编写自己的自定义EasyGridBinder,它继承自GridView并实现这个界面,还是有一种非常简单的方法来做这些我不熟悉的事情?

PS Bonus points if I can write something like 如果我可以写类似的东西,PS奖励积分

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

But I suppose I can figure that out myself after reading up on expressions 但是我想在读完表达之后我可以自己解决这个问题

Follow-up question: Is there no way to get the original data that was input into the gridview and has not been converted to html? 后续问题:是否无法获取输入到gridview中的原始数据并且尚未转换为html? If a field received as input an empty string the cell seems to contain " " so is there no way to distinguish between an input of an empty string and a space? 如果一个字段作为输入接收一个空字符串,则该单元格似乎包含“”,那么是否无法区分空字符串的输入和空格? If this is indeed the case then I probably WILL end up re-implementing most of GridView's functionality. 如果确实如此,那么我可能会最终重新实现GridView的大部分功能。

LinqDataSource allows you to specify your object as the backing store for the data source. LinqDataSource允许您将对象指定为数据源的后备存储。 You'd then bind the GridView to that datasource. 然后,您将GridView绑定到该数据源。 It's a bit more declaration in the .aspx, but it's less code to maintain later as feature bloat brings you closer and closer to reimplementing GridView. 它在.aspx中有一点声明,但随着功能膨胀使你越来越接近重新实现GridView,它的代码更少。

If you set the GridViews AutoGenerateColumns property to false it will only generated the columns you specify. 如果将GridViews AutoGenerateColumns属性设置为false,则它将仅生成您指定的列。 You do that by creating BoundFields and adding them to the Gridview Columns collection. 您可以通过创建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();

I had written this explanation up in the comments but figured I'd move it out to where it was more visible and add a code example: 我在评论中写了这个解释,但想到我会把它移到更明显的位置并添加一个代码示例:

To make the above dynamic create a GridViewDisplayAttribute class which inherits from Attribute. 为了使上面的动态创建一个继承自Attribute的GridViewDisplayAttribute类。 Give GridViewDisplayAttribute a HeaderText property. 为GridViewDisplayAttribute提供HeaderText属性。 Decorate the properties of of T specifying HeaderText. 装饰T的属性,指定HeaderText。 By iterating the properties of T creating BoundFields for each decorated property utilizing HeaderText. 通过迭代使用HeaderText为每个修饰属性创建BoundFields的T的属性。

Quick untested code example: 快速未经测试的代码示例:

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();

I would recommend using extension methods to add the required behavior. 我建议使用扩展方法来添加所需的行为。 The only downside to that is that you can't add "Values" as a property. 唯一的缺点是你不能添加“值”作为属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用Backround工作者还是编写自己的线程 - Should I use a Backround worker or write my own threads 我应该根据自己的情况选择哪种CMS? - Which kind of CMS should I choose according to my own scenario? 我应该实现自己的连接池方案吗? - Should I implement my own connection pooling scheme? 我应该推出自己的ParseInt32版本吗? - Should I roll my own version of ParseInt32? 我应该在自己的非LINQ代码中使用DuplicateKeyException吗? - Should I use DuplicateKeyException in my own non-LINQ code? 我将如何编写自己的FirstOrDefaultAsync()方法 - How would I write my own FirstOrDefaultAsync() method 我是否应该出于其他原因重用Microsoft.NET属性,还是应该创建自己的属性? - Should I reuse Microsoft.NET attributes for other reasons or should I create my own attributes? 如何编写我自己的AuthorizeTag? - How to write my own AuthorizeTag? 我应该如何用C ++编写我的lib以便在.NET中使用? - How should I write my lib in C++ for use in .NET? 我应该抛出自己的ArgumentOutOfRangeException还是从下面冒出一个泡泡? - Should I throw my own ArgumentOutOfRangeException or let one bubble up from below?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM