This is a migrated thread and some comments may be shown as answers.

How to create a grid from code behind?

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nubia Stefania
Top achievements
Rank 1
Nubia Stefania asked on 26 Nov 2012, 04:10 PM
Hello, I have a problem when creating a source-grid is not shown because at runtime, also when I try agrearle a template to a column I get error, I wonder if I could guide you in this.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Nov 2012, 04:03 AM
Hi,

Please take a look into the following code snippet.

ASPX:
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Customers"
    runat="server"></asp:SqlDataSource>

C#:
protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.AutoGenerateColumns = false;
    grid.DataSourceID = "SqlDataSource1";
    string templateColumnName = "ContactName";
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
    templateColumn.HeaderText = templateColumnName;
    GridBoundColumn boundColumn1 = new GridBoundColumn();
    boundColumn1.DataField = "ContactName";
    boundColumn1.UniqueName = "ConactName";
    boundColumn1.HeaderText = "Bound Column";
    grid.MasterTableView.Columns.Add(templateColumn);
    grid.MasterTableView.Columns.Add(boundColumn1);
    grid.AllowPaging = true;
    grid.PageSize = 3;
    grid.Skin = "Outlook";
    PlaceHolder1.Controls.Add(grid);
}
private class MyTemplate : ITemplate
{
    protected LiteralControl lControl;
    protected RequiredFieldValidator validatorTextBox;
    protected HyperLink searchGoogle;
    protected TextBox textBox;
    protected CheckBox boolValue;
    private string colname;
    public MyTemplate(string cName)
    {
        colname = cName;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        lControl = new LiteralControl();
        lControl.ID = "lControl";
        lControl.DataBinding += new EventHandler(lControl_DataBinding);
        textBox = new TextBox();
        textBox.ID = "templateColumnTextBox";
        validatorTextBox = new RequiredFieldValidator();
        validatorTextBox.ControlToValidate = "templateColumnTextBox";
        validatorTextBox.ErrorMessage = "*";
        searchGoogle = new HyperLink();
        searchGoogle.ID = "searchGoogle";
        searchGoogle.DataBinding += new EventHandler(searchGoogle_DataBinding);
        boolValue = new CheckBox();
        boolValue.ID = "boolValue";
        boolValue.DataBinding += new EventHandler(boolValue_DataBinding);
        boolValue.Enabled = false;
        Table table = new Table();
        TableRow row1 = new TableRow();
        TableRow row2 = new TableRow();
        TableCell cell11 = new TableCell();
        TableCell cell12 = new TableCell();
        TableCell cell21 = new TableCell();
        TableCell cell22 = new TableCell();
        row1.Cells.Add(cell11);
        row1.Cells.Add(cell12);
        row2.Cells.Add(cell21);
        row2.Cells.Add(cell22);
        table.Rows.Add(row1);
        table.Rows.Add(row2);
        cell11.Text = colname + ": ";
        cell12.Controls.Add(lControl);
        cell21.Text = "Search Google for: ";
        cell22.Controls.Add(searchGoogle);
        container.Controls.Add(textBox);
        container.Controls.Add(validatorTextBox);
        container.Controls.Add(table);
        container.Controls.Add(new LiteralControl("<br />"));
        container.Controls.Add(boolValue);
    }
    void boolValue_DataBinding(object sender, EventArgs e)
    {
        CheckBox cBox = (CheckBox)sender;
        GridDataItem container = (GridDataItem)cBox.NamingContainer;
        //cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"];
    }
    void searchGoogle_DataBinding(object sender, EventArgs e)
    {
        HyperLink link = (HyperLink)sender;
        GridDataItem container = (GridDataItem)link.NamingContainer;
        link.Text = ((DataRowView)container.DataItem)[colname].ToString();
        link.NavigateUrl = "http://www.google.com/search?hl=en&q=" + ((DataRowView)container.DataItem)["ContactName"].ToString() + "&btnG=Google+Search";
    }
    public void lControl_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Nubia Stefania
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or