Figure 2
Figure 2
Figure 2 DataNavigator Properties and Methods

Property/Method
Description
ConnectionString
The connection string to use to access the data source.
TableName
The name of the table to navigate through.
DataKeyField
Indicates the name of a table column with unique values.
SearchKeyField
Indicates the name of the column to use for sort and search.
SearchKeyText
Indicates the text you sort and search by. For example, if SearchKeyField is companyname, you might want to use "Company name" as the value of this property. This feature makes the control's interface more user friendly.
CurrentRecordIndex
Gets and sets the current record to display.
DataBind
Downloads data and refreshes the control's user interface.
Figure 4 Initialize Grid
private void InitializeGridComponent()
{
    // Default settings for pagination
    m_grid.AllowCustomPaging = true;
    m_grid.AllowPaging = true;
    m_grid.PageSize = 1;
    m_grid.Width = Width;
    m_grid.PagerStyle.Position = PagerPosition.Top;
    m_grid.PagerStyle.NextPageText = "4";
    m_grid.PagerStyle.PrevPageText = "3";
    m_grid.PagerStyle.Font.Name = "webdings";
    m_grid.PagerStyle.Font.Size = FontUnit.Small;
    m_grid.PagerStyle.ForeColor = Color.Black;
            
    // Other visual default settings
    m_grid.BackColor = Color.White;
    m_grid.BorderWidth = (Unit) 0;
    m_grid.ForeColor = Color.Black;
    m_grid.Font.Size = FontUnit.Point(8);
    m_grid.Font.Name = "Verdana";
    m_grid.HeaderStyle.BackColor = Color.Navy;
    m_grid.HeaderStyle.ForeColor = Color.White;
    m_grid.FooterStyle.Font.Size = FontUnit.XXSmall;
    m_grid.FooterStyle.ForeColor = Color.Red;
    m_grid.ShowFooter = false;
        
    // Columns #0
    m_grid.AutoGenerateColumns = false;
    TemplateColumn tc = new TemplateColumn();
    tc.ItemTemplate = new DataNavigatorItemTemplate();
    m_grid.HeaderStyle.BackColor = Color.Navy;  
    m_grid.Columns.Add(tc);

    // Events
    m_grid.PageIndexChanged += new 
      DataGridPageChangedEventHandler(OnPageIndexChanged);
    m_grid.ItemCreated += new DataGridItemEventHandler(OnItemCreated);
}
Figure 5 Pager Bar
private void OnItemCreated(Object sender, DataGridItemEventArgs e)
{
    // Get the item type
    ListItemType lit = e.Item.ItemType;

    // PAGER BAR
    if (lit == ListItemType.Pager)
        CustomizePagerBar(e);
}

private void CustomizePagerBar(DataGridItemEventArgs e)
{
    // Assumes the pager style is NextPrev
    TableCell pager = (TableCell) e.Item.Controls[0];
    ControlCollection ctls = pager.Controls;

    // Add tooltips to next/prev buttons
    ((WebControl)ctls[0]).ToolTip = "Go to previous record";
    ((WebControl)ctls[2]).ToolTip = "Go to next record";

    // Determine whether FIRST/LAST buttons must be rendered 
    // with links or labels
    bool bFirstEnabled = (ctls[0] is LinkButton);
    bool bLastEnabled = (ctls[2] is LinkButton);

    // Add << (first) 
    ctls.AddAt(0, AddLinkLabelButtonFirstPage(bFirstEnabled));
    ctls.AddAt(1, new LiteralControl("&nbsp;"));

    // Add >> (last)
    ctls.Add(new LiteralControl("&nbsp;"));
    ctls.Add(AddLinkLabelButtonLastPage(bLastEnabled));
}

private Control AddLinkLabelButtonFirstPage(bool bEnabled)
{
    if (bEnabled)
    {
        LinkButton lnkFirst = new LinkButton();
        lnkFirst.Text = "9";
        lnkFirst.ForeColor = m_grid.PagerStyle.ForeColor;
        lnkFirst.ToolTip = "Go to first record";
        lnkFirst.Click += new EventHandler(GoToFirstPage);
        return lnkFirst;
    }
    else
    {
        Label lnkFirst = (Label) new Label();
        lnkFirst.Text = "9";
        lnkFirst.ForeColor = m_grid.PagerStyle.ForeColor;
        return lnkFirst;
    }
}

private Control AddLinkLabelButtonLastPage(bool bEnabled)
{
    if (bEnabled)
    {
        LinkButton lnkLast = new LinkButton();
        lnkLast.Text = ":";
        lnkLast.ForeColor = m_grid.PagerStyle.ForeColor;
        lnkLast.ToolTip = "Go to last record";
        lnkLast.Click += new EventHandler(GoToLastPage);
        return lnkLast;
    }
    else
    {
        Label lnkLast = (Label) new Label();
        lnkLast.Text = ":";
        lnkLast.ForeColor = m_grid.PagerStyle.ForeColor;
        return lnkLast;
    }
}
Figure 7 DataNavigatorItemTemplate
public class DataNavigatorItemTemplate : ITemplate
{
    public void InstantiateIn(Control container) 
    {
PlaceHolder ph = new PlaceHolder();
ph.DataBinding += new EventHandler(BindTableColumns);
container.Controls.Add(ph);
    }

    private void BindTableColumns(Object sender, EventArgs e)
    {
    PlaceHolder ph = (PlaceHolder) sender;
    DataGridItem container = (DataGridItem) ph.NamingContainer;
    DbDataRecord dbdr = (DbDataRecord) container.DataItem;
    Table t = new Table();
    :
    ph.Controls.Add(t);
    }
}

Home
Current Issue
Topics
Issues
Columns
Downloads
Subscribe
Contact Us

MSDN Magazine