Karthik
Karthik

Reputation: 207

AutoGenerateColumns="False" Causes Empty RadGrid on Databind

If AutoGenerateColumns="True" then the grid will bind with the dataset and shows the data, but if set to false will not bind and will show the NoRecords value, even though the datatable has rows.

What causes this, and how can I fix it?

pas.MasterTableView.AutoGenerateColumns =false;
DataTable dt = new DataTable();

dt.Columns.Add("SNo");
dt.Columns.Add("Name");
dt.Columns.Add("Add");

DataRow dsa = dt.NewRow();

dsa["SNo"] = "1";
dsa["Name"] = "Karthik";
dsa["Add"] = "Hyd";

dt.Rows.Add(dsa);

dsa = dt.NewRow();
dsa["SNo"] = "2";
dsa["Name"] = "krishna";
dsa["Add"] = "Hyd";
dt.Rows.Add(dsa);

dsa = dt.NewRow();
dsa["SNo"] = "3";
dsa["Name"] = "kailas";
dsa["Add"] = "Hyd";
dt.Rows.Add(dsa);

dsa = dt.NewRow();
dsa["Sno"] = "4";
dsa["Name"] = "Billa";
dsa["Add"] = "Hyd";
dt.Rows.Add(dsa);

dsa = dt.NewRow();
dsa["Sno"] = "5";
dsa["Name"] = "asdf";
dsa["Add"] = "qwer";

dt.Rows.Add(dsa);

pas.DataSource = dt;
pas.DataBind();

Upvotes: 2

Views: 3022

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34846

The AutoGenerateColumns property being set to True instructs the grid to use whatever columns are returned from the database in your grid, so if you turn this off (False), then the grid will have no columns unless you define them, like this:

<Columns>
    <telerik:GridBoundColumn DataField="ProductID" 
                             DataType="System.Int32" 
                             HeaderText="Product ID"
                             SortExpression="ProductID" 
                             UniqueName="ProductID">
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn DataField="ProductName" 
                             HeaderText="Product Name" 
                             SortExpression="ProductName"
                             UniqueName="ProductName">
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn DataField="UnitPrice" 
                             DataType="System.Decimal" 
                             HeaderText="Unit Price"
                             SortExpression="UnitPrice" 
                             UniqueName="UnitPrice">
    </telerik:GridBoundColumn>
</Columns>

You would use AutoGenerateColumns=False if you did not want all of the columns from the database query in the grid or if you did not like the names of the columns, because it uses the database field name (i.e. SYSTEM_ID).

Upvotes: 2

Raghubar
Raghubar

Reputation: 2788

If you set the AutoGenerateColumns property to false and you do not have manually defined columns there will be no columns in RadGrid. Since there are no columns there will be no records because records are cells of given columns. Since there are no rows in the Grid the NoRecordsToDisplay message will be shown. Check this link to view add column manualy.

click here for deatils

Upvotes: 1

Related Questions