RadGrid with autogenerated columns has NO columns object!

1 Answer 242 Views
Grid
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
Deasun asked on 25 Jul 2022, 01:57 PM

So I set a datasoruce to the datatabler and then do .Databind().

The .items is full but the .columns is empty!!

Why is the .Columns empty I thought it was suppose to autogenerate the columns from the fields in the table!!

Confused!!

RadGrid objTheGrid

DataTable dtResult = new DataTable();

 dtResult = objLineTrendsTools.Reports.Provider.Grids.getDefaultView(objParameters);

 objTheGrid.DataSource = dtResult;
                objTheGrid.DataBind();

If ( objTheGrid.items.count >  0 )  // this works has items

if ( objTheGrid.columns.count > 0 )  // this doesnt works as the Columns is empty .Count = 0

 

Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
commented on 25 Jul 2022, 02:01 PM

see , this is the watch:

Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
commented on 25 Jul 2022, 02:04 PM

and the aspx code:

<telerik:RadGrid ID="grdData" runat="server" Height="300px" Skin="Metro" ShowFooter="false" Font-Names="Arial,Verdana,Tahoma" Font-Size="11px" OnSelectedIndexChanged="grdData_SelectedIndexChanged">
                                <MasterTableView AutoGenerateColumns="true" TableLayout="Fixed">
                                    <HeaderStyle CssClass="ColumnHeaderCellStyle" />
                                </MasterTableView>
                                <ClientSettings EnablePostBackOnRowClick="true" >
                                    <Scrolling AllowScroll="true" UseStaticHeaders="true" FrozenColumnsCount="2" />                     
                                    <Selecting AllowRowSelect="true"  />
                                </ClientSettings>
                            </telerik:RadGrid>

 

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 27 Jul 2022, 09:46 AM | edited on 27 Jul 2022, 09:47 AM

Hi Deasun,

When the Columns are autogenerated, the Columns element does not contain any columns and the property will remain empty.

However, you can use the RenderColumns property which is an Array holding the Columns that are to be rendered when the Page loads.

 

Try the following example

 

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView AutoGenerateColumns="true" DataKeyNames="OrderID">
        <Columns>
            <%--There are no Columns in the Column element--%>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

<telerik:RadButton runat="server" ID="RadButton1" Text="Postback" AutoPostBack="true" OnClick="RadButton1_Click" />

 

 

C# code for data binding

 

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = OrdersTable(); 
}

private DataTable OrdersTable()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
    dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
    dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
    dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));

    dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };

    for (int i = 0; i < 70; i++)
    {
        int index = i + 1;

        DataRow row = dt.NewRow();

        row["OrderID"] = index;
        row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
        row["Freight"] = index * 0.1 + index * 0.01;
        row["ShipName"] = "Name " + index;
        row["ShipCountry"] = "Country " + index;

        dt.Rows.Add(row);
    }

    return dt;
}

protected void RadButton1_Click(object sender, EventArgs e)
{
    int colCount1 = RadGrid1.Columns.Count;
    int colCount2 = RadGrid1.MasterTableView.Columns.Count;
    string orderDateColHeaderText = RadGrid1.MasterTableView.GetColumn("OrderDate").HeaderText;
    int itemsCount = RadGrid1.Items.Count;

    GridColumn[] renderColumns = RadGrid1.MasterTableView.RenderColumns;

    int renderColumnCount = renderColumns.Length;
}

 

 

Result

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
commented on 28 Jul 2022, 10:57 AM

Thank you.
Tags
Grid
Asked by
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
Answers by
Attila Antal
Telerik team
Share this question
or