Monday, June 13, 2011

GridView Unleashed

  • DataKeys (using RowIndex)
    DataKeyNames
    DataItem
    DataItemIndex
    DataMember
    Control.NamingContainer
    Empty GridView with Column Headers
    Javascript validation for controls in the gridview Cells (done)
    TextBox Validation : ServerSide (using RegularExpressionValidator WebServer Control)
    GridViews -> Datakeys
    DataKeys[RowIndex] -> DataKey values for a Row
    DataKey.Values["KeyName"] -> DataKey value corressponding to a particular DataKeyName
     

  • To make Empty GridView show the column headers,  create a custom GridView Control by deriving from the Standard GridView control and override "CreateChildControls( )" mehod



        protected GridViewRow _footerRow2;
        protected GridViewRow _headerRow2;

        protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            // Call base method and get number of rows
            int numRows = base.CreateChildControls(dataSource, dataBinding);

            if (numRows == 0)
            {
                //no data rows created, create empty table
                //create table
                Table table = new Table();
                table.ID = this.ID;

                //convert the exisiting columns into an array and initialize
                DataControlField[] fields = new DataControlField[this.Columns.Count];
                this.Columns.CopyTo(fields, 0);

                if (this.ShowHeader)
                {
                    //create a new header row
                    _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                    this.InitializeRow(_headerRow2, fields);
                    _headerRow2.EnableTheming = true;
                    table.Rows.Add(_headerRow2);
                }

                if (this.ShowFooter)
                {
                    //create footer row
                    _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

                    this.InitializeRow(_footerRow2, fields);
                    _footerRow2.EnableTheming = true;
                    table.Rows.Add(_footerRow2);
                }

                this.Controls.Clear();
                this.Controls.Add(table);
            }

            return numRows;
        }

No comments:

Post a Comment