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

Swapping between grid column and autogenerated columns

6 Answers 221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Purojit
Top achievements
Rank 1
Purojit asked on 21 Jun 2013, 08:16 AM
Hi,

I have a Rad Grid with grid-bound columns and auto-generated columns. Now I want to swap a particular grid-bound field to the end of auto-generated columns.
I have visited the telerik forum. Only code I have found out is this:

protected void RadGrid1_PreRender(object sender, System.EventArgs e)
    {
     RadGrid1.MasterTableView.SwapColumns("AutoGeneratedColumn", "PreDefinedColumn");
    }

As the auto-generated columns are dynamically taking data from database , it is not possible to give the name of the last auto-generated column name. Also to note,I have given the  name of an auto-generated column.But it is not working.

Please help.

Regards
Purojit

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Jun 2013, 09:05 AM
Hi,

I have tried the swapping with autogenerated and bound columns,It works fine at my end.Please try the code snippet.For more evaluation,please provide your code.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"  AutoGenerateColumns="true" onprerender="RadGrid1_PreRender">
   <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShippedDate" HeaderText="ShippedDate" UniqueName="ShippedDate">
            </telerik:GridBoundColumn>                
          </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
 {
  RadGrid1.MasterTableView.SwapColumns("id", "ShippedDate");//("AutoGeneratedColumnName","BoundColumn")
 }

Thanks,
Princy
0
Purojit
Top achievements
Rank 1
answered on 21 Jun 2013, 09:28 AM
Hi Princy,

Thanks for reply, but it is really not clear that what do I put on "id" on auto-generated columns. My autogenerated columns are populating directly from database.
Please help.

Regards
Purojit
0
Jayesh Goyani
Top achievements
Rank 2
answered on 21 Jun 2013, 10:20 AM
Hello,

Please try with below code snippet.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender">
       <MasterTableView>
           <Columns>
               <telerik:GridBoundColumn DataField="ID" UniqueName="Total" HeaderText="Total">
               </telerik:GridBoundColumn>
           </Columns>
       </MasterTableView>
   </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
           new { ID = 1, Name ="Name1"},
           new { ID = 2, Name = "Name2"},
           new { ID = 3, Name = "Name3"},
            new { ID = 4, Name = "Name4"},
           new { ID = 5, Name = "Name5"},
           new { ID = 26, Name = "Name26"}
       };
 
       RadGrid1.DataSource = data;
 
   }
   protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       RadGrid1.MasterTableView.SwapColumns("Total", "Name");
   }


Thanks,
Jayesh Goyani
0
Purojit
Top achievements
Rank 1
answered on 27 Jun 2013, 07:45 AM
Hi Jayesh,


Thanks for your help. My concern is that the auto-generated  column names are to be generated as per database and has to be made dynamic. or else in case there is a change made or an addition occurs in database from the client side on the auto-generated columns, we have to implement this( new { ID = .., Name = "Name.."}) again.

Regards
Purojit
0
Princy
Top achievements
Rank 2
answered on 27 Jun 2013, 08:39 AM
Hi,

You can swap any two columns using
RadGrid1.MasterTableView.SwapColumns("Column1","Column2") in the PreRender() event of the radgrid. The Order is not a problem.I have tried it on my side by using Autogenerated and Bound column.

Here CustomerID is a boundColumn and ShipName is AutoGenerated.
ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"OnPreRender="RadGrid2_PreRender" AllowPaging="true">
           <MasterTableView AutoGenerateColumns="True">
                <Columns>
                    <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID">
                    </telerik:GridBoundColumn>               
                </Columns>
          </MasterTableView>         
</telerik:RadGrid>

C#:
protected void RadGrid2_PreRender(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.SwapColumns("CustomerID","ShipName"); // to swap two columns
}

Thanks,
Princy
0
IT Invoices
Top achievements
Rank 1
answered on 02 Oct 2013, 09:28 PM
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       If RadGrid1.MasterTableView.AutoGeneratedColumns.Length > 2 Then
           RadGrid1.MasterTableView.SwapColumns("columnName1", "columnName2")
       End If
   End Sub
 On the Page Load Method Swap Column which works

note: Dynamically Autogenerated Column is not accessible in the pre_render method.
Tags
Grid
Asked by
Purojit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Purojit
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
IT Invoices
Top achievements
Rank 1
Share this question
or