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

Show columns without datasource

1 Answer 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 14 Jul 2010, 04:32 AM
I am doing a mock up of a web app and I need to display the columns of the Grid without a Datasource or records, basically I just need the grid to display for look and feel purposes. So far I can't get anything to show but a blank space.

Thanks!
Sam

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 14 Jul 2010, 06:54 AM
Hi Sam,

RadGrid does not support "unbound mode", i.e. if you don't bind it to some datasource, the control will not render a <table>. You can have columns with no rows as in the example below (declarative columns, AutoGenerateColumns="false" and ShowHeadersWhenNoRecords="true"), but I doubt that this is your purpose in this case. I recommend you to use a dummy datasource.

(change the value of rowsNum in the demo below)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<script runat="server">
 
    protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        int colsNum = 4;
        int rowsNum = 0;
        string colName = "Column";
 
        for (int j = 1; j <= colsNum; j++)
        {
            dt.Columns.Add(String.Format("{0}{1}", colName, j));
        }
 
        for (int i = 1; i <= rowsNum; i++)
        {
            dr = dt.NewRow();
 
            for (int k = 1; k <= colsNum; k++)
            {
                dr[String.Format("{0}{1}", colName, k)] = String.Format("{0}{1} Row{2}", colName, k, i);
            }
            dt.Rows.Add(dr);
        }
 
        (sender as RadGrid).DataSource = dt;
    }
 
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>RadControls</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
 
<telerik:RadGrid
    ID="RadGrid1"
    runat="server"
    AllowPaging="true"
    AutoGenerateColumns="false"
    OnNeedDataSource="RadGrid_NeedDataSource">
    <MasterTableView ShowHeadersWhenNoRecords="true">
        <Columns>
            <telerik:GridBoundColumn HeaderText="My Column 1" DataField="Column1" />
            <telerik:GridBoundColumn HeaderText="My Column 2" DataField="Column2" />
            <telerik:GridBoundColumn HeaderText="My Column 3" DataField="Column3" />
            <telerik:GridBoundColumn HeaderText="My Column 4" DataField="Column4" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
</form>
</body>
</html>


Sincerely yours,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Sam
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or