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 footerif (numRows == 0){
//create tableTable table = new Table(); table.ID = this.ID; //create a new header row based on the header that is usually createdGridViewRow 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 tableDataControlField[] 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 Designrow = 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 tabletable.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á!@!