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

Create radgrid Dynamically and value to bind

6 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tamim
Top achievements
Rank 1
Tamim asked on 16 May 2014, 05:17 AM
Hi,

   how to create 1 or more radgrid dynamically in page load and grid needdatasource grid bind also databound use............

Advance & Thanks,
Ansari.

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 16 May 2014, 05:30 AM
Hi Tamim,

You can refer the following articles which will give you the required information.
Creating a RadGrid Programmatically
Advanced Data-binding (Using NeedDataSource Event)

Thanks,
Princy
0
Tamim
Top achievements
Rank 1
answered on 16 May 2014, 05:51 AM
Thanks princy for ur reply..

   One more question is there...

  Dynamic create Button(Expand and collapse) function how to working...
 
  See my screen shot..

 1)  I want first each Project assign to tab(bar) default collapse when i click Expand button to view MY PROGRESS CLAIM (2), MY   PAYMENT CERTIFICATE (1)...
2)  In MY PROGRESS CLAIM i click Prepare to set Link.
  

Thanks,
Ansari.
0
Princy
Top achievements
Rank 1
answered on 19 May 2014, 05:09 AM
Hi Tamim,

From your screenshot I guess you can use grouping. Please try the following code snippet. Elaborate on your requirement if this doesn't help.

C#:
//Group Expression dynamically
RadGrid1.MasterTableView.GroupsDefaultExpanded = false;
GridGroupByExpression expression = new GridGroupByExpression();
GridGroupByField gridGroupByField = new GridGroupByField();
  
gridGroupByField = new GridGroupByField();
gridGroupByField.FieldName = "ShipCountry";
gridGroupByField.HeaderText = "ShipCountry";
gridGroupByField.HeaderValueSeparator = ":";
gridGroupByField.FormatString = " ";
  
expression.SelectFields.Add(gridGroupByField);
gridGroupByField.FieldName = "ShipCountry";
expression.GroupByFields.Add(gridGroupByField);
RadGrid1.MasterTableView.GroupByExpressions.Add(expression);
  
//To display the total count of childitems on header
void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridGroupHeaderItem groupHeaderItem in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        if (!(groupHeaderItem.DataCell.Text.Contains("Child Items:")))
        {
            groupHeaderItem.DataCell.Text = groupHeaderItem.DataCell.Text +
           " Child Items: " + groupHeaderItem.GetChildItems().Length.ToString();
        }
    }
}

Thanks,
Princy
0
Tamim
Top achievements
Rank 1
answered on 19 May 2014, 10:42 AM
Thanks Princy for ur reply...

 See my screen shot..

  In Countrywise sub Node in State Wise....

 I want one more sub node in State wise

 For Example Nation --> State --> District

Thanks,
Ansari.
 
0
Tamim
Top achievements
Rank 1
answered on 19 May 2014, 10:42 AM
Hi Prince any Examples.....
0
Princy
Top achievements
Rank 1
answered on 20 May 2014, 05:53 AM
Hi Tamim,

From your screenshot I guess you want to use a Hierarchy RadGrid. Please check the below is a sample code snippet and the attached screenshot.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="CountryID" HierarchyLoadMode="ServerBind" Name="Country">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="StateID" Name="State">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="StateID" Name="District">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="DistrictID" DataField="DistrictID" UniqueName="DistrictID">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="DistrictName" DataField="DistrictName" UniqueName="DistrictName">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="StateID" DataField="StateID" UniqueName="StateID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="StateName" DataField="StateName" UniqueName="StateName">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn SortExpression="CountryID" HeaderText="CountryID" DataField="CountryID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn SortExpression="CountryName" HeaderText="CountryName" DataField="CountryName">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        RadGrid1.DataSource = GetDataTable("Select * from Country");
    }
}
 
protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
    GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
    switch (e.DetailTableView.Name)
    {
        case "State":
            {
                string CountryID = dataItem.GetDataKeyValue("CountryID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM State WHERE CountryID = '" + CountryID + "'");
                break;
            }
        case "District":
            {
                string StateID = dataItem.GetDataKeyValue("StateID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM District WHERE StateID = '" + StateID + "'");
                break;
            }
    }
}
 
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}

Thanks,
Princy
Tags
Grid
Asked by
Tamim
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Tamim
Top achievements
Rank 1
Share this question
or