How to add rows programmatically?

2 replies. Last post: June 6, 2011 9:40 PM by Riendy Setiadi
Tags :
  • New Discussion
  • New Question
  • New Product Feedback
mpeetMember

I am new to this control.  I want to add rows to a WebGrid programmatically.  I want the WebGrid to be unbound from any database.  I want to use asp.net controls within the cells, so I am using templates.  I keep getting a NullReferenceException.

Here is a simple example demonstrating my problem:

<ISWebGrid:WebGrid ID="WebGrid1" Height="300px" Width="100%" UseDefaultStyle="True" runat="server" >
    <RootTable>
        <Columns>
            <ISWebGrid:WebGridColumn Name="col1" Bound="false" ColumnType="Template">
                <CellTemplate>
                    <asp:TextBox ID="txt1" runat="server" />
                </CellTemplate>
            </ISWebGrid:WebGridColumn>
        </Columns>
    </RootTable>
</ISWebGrid:WebGrid>

 Code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        WebGrid1.InitializeRow += new RowEventHandler(WebGrid1_InitializeRow);
        WebGridRow row = WebGrid1.RootTable.CreateRow();
        WebGrid1.RootTable.Rows.Add(row);
    }
    void WebGrid1_InitializeRow(object sender, RowEventArgs e)
    {
        if (e.Row.Type == RowType.Record)
        {
            WebGridRow row = (WebGridRow)e.Row;
            WebGridCellTemplateContainer cell = row.Cells[0].GetTemplateContainer();    // <-- always comes back null
            TextBox txt1 = (TextBox)cell.FindControl("txt1");
        }
    }


The problem is that the call to GetTemplateContainer() always returns null and i can't figure out why.

What am I doing wrong?

Thanks in advance,

Mike

All times are GMT -5. The time now is 2:27 AM.
Previous Next