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

Pivot for radGrid

3 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Caro
Top achievements
Rank 1
Caro asked on 14 Jun 2012, 12:13 PM
Hi :)

I tried to implement a pivot for radgrid ... And I have some problems ....

I'd like my line's name are not the field of my database (for example, now I have "db_name" and I want "Name").
I'd like to make a header for all the grid with "User Informations"

This is my code :

ASP :

<telerik:RadGrid runat="server" ID="view" 
  ShowStatusBar="true"
  AutoGenerateColumns="false"
  Visible="false">
   
  <ClientSettings>
     <Selecting AllowRowSelect="True"/>
  </ClientSettings>
 
  <GroupingSettings CaseSensitive="false" />
     
  <MasterTableView  ShowHeadersWhenNoRecords="true" DataKeyNames="Id"  ClientDataKeyNames="Id">
 
  <Columns>
 
       <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="User Name"
       AutoPostBackOnFilter="true"  Visible="true">
       </telerik:GridBoundColumn>
 
       <telerik:GridBoundColumn DataField="Phone" UniqueName="Phone" HeaderText="User Phone"
       AutoPostBackOnFilter="true"  Visible="true">
       </telerik:GridBoundColumn>
 
       <telerik:GridBoundColumn DataField="Email" UniqueName="Email" HeaderText="User Email"
       AutoPostBackOnFilter="true"  Visible="true">
       </telerik:GridBoundColumn>
  </Columns>
  </MasterTableView>   
</telerik:RadGrid>
 
<br />
 
<telerik:RadGrid runat="server" ID="view2" 
  ShowStatusBar="true"
  Visible="true">
   
  <ClientSettings>
     <Selecting AllowRowSelect="True"/>
  </ClientSettings>
 
  <GroupingSettings CaseSensitive="false" />
     
  <MasterTableView  ShowHeader="false" >
   
  </MasterTableView>
  </telerik:RadGrid>


C#

protected void Page_Load(object sender, EventArgs e)
 {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
 
 }
 
 
        private DataTable PivotTable(DataTable origTable)
        {
 
            DataTable newTable = new DataTable();
            DataRow dr = null;
            //Add Columns to new Table
            for (int i = 0; i <= origTable.Rows.Count; i++)
            {
                newTable.Columns.Add(new DataColumn(origTable.Columns[i].ColumnName, typeof(String)));
            }
 
            //Execute the Pivot Method
            for (int cols = 0; cols < origTable.Columns.Count; cols++)
            {
                dr = newTable.NewRow();
                for (int rows = 0; rows < origTable.Rows.Count; rows++)
                {
                    if (rows < origTable.Columns.Count)
                    {
                        dr[0] = origTable.Columns[cols].ColumnName; // Add the Column Name in the first Column
                        dr[rows + 1] = origTable.Rows[rows][cols];
                    }
                }
                newTable.Rows.Add(dr); //add the DataRow to the new Table rows collection
            }
            return newTable;
        }
 
        private void BindGridView()
        {
            String str = Request.RawUrl;
            if (!IsPostBack)
            {
                if (str.Contains("?a"))
                {
                    Int32 url_id = Convert.ToInt32(Request.Params["a"]);
                    user_id = url_id;
                    DataTable dt = new DataTable();
                    SqlConnection cn = new SqlConnection("");
                    SqlCommand cmd = new SqlCommand("user_data", cn);
                    cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
                    cmd.Parameters["@id"].Value = url_id;
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
                    sqlDa.Fill(dt);
 
                    try
                    {
                        cn.Open();
                        if (dt.Rows.Count > 0)
                        {
                            //Bind the First GridView with the original data from the DataTable
                            view.DataSource = dt;
                            view.DataBind();
 
                            //Pivot the Original data from the DataTable by calling the
                            //method PivotTable and pass the dt as the parameter
 
                            DataTable pivotedTable = PivotTable(dt);
                            view2.DataSource = pivotedTable;
                            view2.DataBind();
                        }
                    }
 
                    catch (SqlException ex)
                    {
                        //...
                    }
                    finally
                    {
                        if (cn.State != ConnectionState.Closed)
                        {
                            cn.Close();
                            cmd.Parameters.Clear();
                        }
                    }
                }
            }
        }

Thanks for the help :)

3 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 19 Jun 2012, 02:12 PM
Hi Caro,

I would suggest you to review the online demo below which presents RadGrid bind to pivot data:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/pivot/defaultcs.aspx

Also please note that in the upcoming SP the new RadPivotGrid control will be released.

All the best,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Gajanan
Top achievements
Rank 2
answered on 17 Aug 2015, 10:04 AM
Dear all,
My requirement is Bind the grid dynamic , and add the controls like Text Box And rad Como box at run time by Item value.
so far have done the control adding and now my grid is ready to take values from user , 
but when user selected value from rad combo box (dynamic added control in grid), and click the save button i cannot find the Rad Combo box 
pls suggest me the way how to get the values for dynamically added controls in Rad-grid on button click.
below are the steps i followed
1. Added Grid in Aspx. with Autogenerated Column = true
2. On Item created event added dynamic controls in Grid. 
3. bind the data to ad combo box (dynamic added control in grid) 
4. on save Button i have to save the selected values from rad combo box (dynamic added control in grid), to data base
in above step 1 to 3 are done , but step 4 making me trouble .

below is code for button click.
protected void btnSaveAll_Click(object sender, EventArgs e)
{
    Questionnaire MyQue = Questionnaire.getQuestionnaire(mQuestionnaireid); 
    ArrayList ColumnLst = GetColumnList();
                Dictionary<string,string> RowSplitValue = new Dictionary<string,string>();
                foreach (GridEditableItem Item in rgPivotQuestionnaireResult.MasterTableView.Items)
                {
                    foreach (GridColumn Column in rgPivotQuestionnaireResult.MasterTableView.RenderColumns)
                    {
                        if (ColumnLst.Contains(Column.UniqueName))
                        {
                            //RowSplitValue = new Dictionary<string, string>(Item.SavedOldValues[Column.UniqueName]);
                            if (Item[Column].Controls.Count > 0)
                            {
                                Control ItemControle= new Control();
                                ItemControle = Item[Column].Controls[0];
                                if (ItemControle is TextBox)
                                {
                                    TextBox TxtBx = (TextBox)Item[Column].Controls[0];
                                }
                                if (Item[Column].Controls.Count > 1)
                                {
                                    ItemControle = Item[Column].Controls[1];
                                    if (ItemControle is RadComboBox)
                                    {
                                        RadComboBox RCB = (RadComboBox)Item[Column].Controls[1];
                                          MyQue .Answervalue = RCB.SelectedValue;
                                    }
                                }
                            }
                        }
                    }
                }
   MyQue .Save();
}
0
Viktor Tachev
Telerik team
answered on 18 Aug 2015, 01:08 PM
Hello,

I have replied to your query in the other thread where you have posted. The link is found below.

Please avoid submitting duplicate questions in different threads. Thus, we would be able to answer your queries quicker. Moreover, it is recommended to post unrelated queries in a separate thread. This will make the thread more consistent and helpful to anyone with similar query.



Regards,
Viktor Tachev
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Caro
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Gajanan
Top achievements
Rank 2
Viktor Tachev
Telerik team
Share this question
or