GridView - A great control to extend!

Hi all!

 As you should know, the GridView control is a great control but, what you might not know, is that is really easy to improve the tasks performed by GridView. One task that I was asked a time ago, was the possibility to perform record input using the footer of the GridView. This is a solution for small tables, where the number of fields are fewer than 10, more than that, I suggest a web form to do the action.

 The doubt of the developer that asked me that is the fact that DataGrid supported this with no kind of aditional code when there is no records on the DataSource of the GridView. Well, GridView, as its name suggest is a View so, what does not exists is not show. To perform this task using the GridView, you'll need to create another control, that inherits GridView and override the CreateChildControls method as show below:

protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)

{

int numRows = base.CreateChildControls(dataSource, dataBinding);

//If no data rows created so we will work to create our own header and footer

if (numRows == 0)

{

//create table

Table table = new Table(); table.ID = this.ID;

//create a new header row based on the header that is usually created

GridViewRow row = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

//convert the exisiting columns into an array, initialize and then add the row to the previously created table

DataControlField[] fields = new DataControlField[this.Columns.Count];

this.Columns.CopyTo(fields, 0);

this.InitializeRow(row, fields);

table.Rows.Add(row);

//create footer row that will follow the design and behavior of the footer created on the GridView Design

row = new GridViewRow(-2, -1, DataControlRowType.Footer , DataControlRowState.Normal);

//initialize the row based on the fields added

this.InitializeRow(row, fields);

//adding the row to the previously created table

table.Rows.Add(row);

//adding the table to the control.

this.Controls.Add(table);

}

return numRows;

}

 

 Well this is my first post here and I hope that it can help you!!

 See ya!@!

 Chilá!@!

3 Comments

  • Rather than creating a GridView subclass, you could make your BoundField columns TemplateFields with FooterTemplates defined for each of your columns.

  • It never ceases to amaze me ho wmany different ways people get creative with extending controls. It is important that all developers understand that the sky is the limit and anything is possible with a little ingenuity. Keep it up!

    Also, a nice control set Rodrigo!

  • Rodrigo, I already take a look on your control too! It was some time ago when I was told by another user on codeplex that another grid control exists. My little baby is still in-utero, but you can see something on www.codeplex.com/SuperGRID.

    As Ryan said, the sky is the limit and as a developer I always see something that can get better, so, let´s improve what was done before...

    See ya!@!

Comments have been disabled for this content.