开发者

Binding sclar values in C# grid controls

Recently I have been using both the ASP.Net GridView Control and the WinForms DataGridView to display data dynamically. In both cases I have been us开发者_开发百科ing various generic Lists as the datasource (List<T>). When this list in as a collection of types with properties defined, these controls have no problem binding to a named property, and in the case of the DataGridView will display the properties as headers with the values for each property as the rows.

However when I have a collection of strings or ints for example, these controls have trouble binding to the values contained in the lists. I'm creating my ASP GridView control dynamically so its not defined in the page untill it is needed so I don't think a binding expression will work here, although I'm new to binding expressions so I could be wrong:

GridView grid = new GridView();
grid.AutoGenerateColumns = false;
grid.CssClass = "summaryTable";

grid.Columns.Add(new TemplateField { HeaderText = "Error No.", ItemTemplate = new DataGridAutoNumber(grid) });
grid.Columns.Add(new BoundField { HeaderText = "Error Description", DataField="Value" });


grid.DataSource = validator.ValidationErrors;
grid.DataBind();

In the above example validator.ValidationErrors is a list of strings. In order to get the GridView to bind the string values I had to wrap them in a type I created:

public class ValueItem<T>
{
    T value;

    public ValueItem(T valueIn) { value = valueIn; }

    public T Value { get { return value; } }
}

This type works for both GridView and DataGridView and allows me to create a List<ValueItem<T>>() of any value type so I can bind it to a Grid type control.

Now am I missing something here or do these controls just not work well with collections of value types?

Apologies for the long question!

P.S. As a side note if anyone knows how to create an autonumber column in a GridView in the code not the script, please let me know. My solution was this:

public class DataGridAutoNumber : ITemplate
{
    GridView grid;
    public DataGridAutoNumber(GridView gridIn) { grid = gridIn; }

    #region ITemplate Members

    public void InstantiateIn(Control container)
    {
        container.Controls.Add(new Label{ Text=(grid.Rows.Count+1).ToString()});
    }

    #endregion
} 


you already know how to add Label to GridView.

Here is one logic to add auto numbering i.e. row number to grid

Untested code

protected void gv_DataBound(object sender, EventArgs e)
        {
            int pageIndex = gv.PageIndex;
            int pagesize = 20;
            int count = pagesize * pageIndex;
            foreach (GridViewRow row in gv.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    count ++;
                    Label lbl = row.FindControl("lblAutoNumber") as Label;
                    lbl.Text = count.ToString();
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜